A set of small functions and methods commonly used in c#.net

Source: Internet
Author: User
Tags check character servervariables
1, the DateTime digital type System.DateTime currenttime=new System.DateTime (), 1.1 takes the current year month day time cent the second currenttime=system.datetime.now; 1.2 Take current year int year =currenttime.year;1.3 take current month int month =currenttime.month;1.4 take current day int day =currenttime.day;1.5 take current time int = currenttime.hour;1.6 takes the current sub int =currenttime.minute;1.7 takes the current second int seconds =currenttime.second;1.8 takes the current millisecond int milliseconds = Currenttime.millisecond (variable available in Chinese) 2, int32.parse (variable) int32.parse ("constant") character conversion to 32-bit digital 3, variable. The ToString () character conversion is converted to string 12345. ToString ("n");//Generate 12,345.0012345.tostring ("C");//Generate ¥12,345.0012345.tostring ("E");//Generate 1.234500e+00412345. ToString ("F4");//Generate 12345.000012345.ToString ("x");//Generate 3039 (16 binary) 12345.ToString ("P");//Generate 1,234,500.00%4, variable. Length numeric string Lengths: String str= "China"; int Len = str. Length;//len is a custom variable, str is the variable name of the measured string 5, System.Text.Encoding.Default.GetBytes (variable) loadline converted to bit code such as: byte[] Bytstr = System.Text.Encoding.Default.GetBytes (str); then you can get the bit length: len = bytstr.length;6, System.Text.StringBuilder ("") string addition, (+ The number is the same? such as: System.Text.StringBuilder SB = new System.Text.StringBuilder (""); sb. Append ("Zhonghua"); sb. Append ("The People"); sb. AppEND ("Republic"); 7, variable. Substring (parameter 1, parameter 2), part of the Intercept string, parameter 1 is the left start bit number, and the parameter 2 is the intercept several. such as: string S1 = str. Substring (0,2); 8, String user_ip=request.servervariables["REMOTE_ADDR"]. ToString (); Take remote User IP address 9, through Proxy server take remote user real IP address: if (request.servervariables["Http_via"]!=null) {stringuser_ip= request.servervariables["Http_x_forwarded_for"]. ToString ();} else{stringuser_ip=request.servervariables["REMOTE_ADDR"]. ToString ();} 10, session["variable"]; Access Session value, for example, assignment: session["username"]= "Bush"; Value: Object objname=session["username"]; String strname=objname.tostring (); Empty: Session.removeall (); 11, String str=request.querystring["variable"; the variable is passed by hyperlink. If you are building a hyperlink on any page: <a href=edit.aspx?fbid=23> Click </a> value in edit.aspx page: String str=request.querystring["Fdid"];12, Doc object. createelement ("New node name"); Create an XML document new 13, parent node. AppendChild (child node); Adds a new child node to the parent node of the XML document under 14, parent node. RemoveChild (node), delete node 15, Responseresponse.write ("string"), Response.Write (variable), output to page. Response.Redirect ("URL address"); jumps to the URL specified on page 16, Char. ISWHITESPCE (string variable, number of digits)--Logical type check the specified position is null character, such as: string str= "Chinese people"; Response.Write (Char. Iswhitespace (Str,2)); The result is: True, the first character is 0 bits, and 2 is the third character. 17, Char. Ispunctuation (' character ')--whether the logical check character is a punctuation mark such as: Response.Write (char. Ispunctuation (' A '));//return: False18, (int) ' character ' convert the character to a number, check the code point, note that it is a single quotation mark. such as: Response.Write ((int) ' in ');//The result is a code of medium character: 2001319, (char) code converts a number to a character and checks the character represented by the code. such as: Response.Write ((char) 22269);//Return to the word "state". 20, Trim () clear the string before and after the space 21, string variables. Replace ("substring", "Replace with") string substitution such as: string str= "China"; str=str. Replace ("Country", "central");//Change the State to Response.Write (str);//The output is "center" again as: (This very practical) string str= "This is the <script> script"; str=str. Replace ("<", "<font><</font>");//replaces the left angle bracket with <font> and < and </font> (or &AMP;LT, However, it is estimated that the Response.Write (str) will still be restored after the XML has been stored; Display as: "This is <script> script" if not replaced,<script> will not be displayed, if it is a script that will run, and after substitution, the script will not run. The value of this code is that you can invalidate all the HTML tags in a text, show them all, and protect your interactive site. Implementation: Add your Form submit button script with the following code: string Strsubmit=label1. Text;//label1 is the ID of the control for which you want the user to submit data. Strsubmit=strsubmit.replace ("<", "<font><</font>"), and then save or output strsubmit. The UBB code can also be implemented simply by using this method. 22, Math.max (I,J) Take I and J maximum value such as int x=math.max (5,10);//x will take value 10

23, string comparison is generally used: if (STR1==STR2) {}, but there are other methods:

(1),

String str1; str2

Syntax: STR1. EndsWith (STR2); __ detects if the string str1 is ending with a string str2 and returns a Boolean value. For example:

if (str1. EndsWith (STR2)) {Response.Write ("string str1" is the end of "+str2+");}

(2),

Syntax: STR1. Equals (STR2); __ detects if the string str1 is equal to the string str2, and returns a Boolean value, as in the previous usage.

(3),

Syntax equals (STR1,STR2); __ detects if the string str1 is equal to the string str2, and returns a Boolean value, as in the previous usage.

24, IndexOf (), LastIndexOf ()

Finds the position of the first (last) occurrence of a specified character or string in a string, returning an index value, such as:

Str1. IndexOf ("word");//Find the index value (position) of "word" in str1

Str1. IndexOf ("string");//Find the index value (position) of the first character of the string in str1

Str1. IndexOf ("string", 3,2);//From str1 4th character, look for 2 characters, find the index value of the first character of the string in str1 (position)

25. Insert ()

Inserts the specified character into the string by specifying the index bit. Such as:

Str1. Insert (1, "word"), in str1 the second word prompt insert "word", if str1= "China", after inserting "Chinese character country";

26, PadLeft (), PadRight ()

Add a space to the left (or right) of the string, or specify a char character, so that the string reaches the specified length, such as:

<%

String str1= "Chinese";

Str1=str1. PadLeft (10, "' 1"); No second argument is plus space

Response.Write (STR1); The result is "1111111 Chinese", with a string length of 10

%>

27. Remove ()

Deletes the specified number of characters starting at the specified position

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.