C # common functions-general,

Source: Internet
Author: User

C # common functions-general,

C # common functions → general functions
Reprinted address → http://www.cnblogs.com/superfang/archive/2008/07/02/1233706.html
I used to "original address" in the past → later I found that some of them were not accurate, but I did not write the original address → or "reprinted address ".

 

1. DateTime numeric type System. dateTime currentTime = new System. dateTime (); 1.1 returns the current year, month, day, hour, minute, second currentTime = System. dateTime. now; 1.2: the current year int year = currentTime. year; 1.3 the current month int month = currentTime. month; 1.4 the current day int day = currentTime. day; 1.5 when the current time is int = currentTime. hour; 1.6 get the current minute int minute = currentTime. minute; 1.7 takes the current second int seconds = currentTime. second; 1.8 takes the current millisecond int millisecond = currentTime. millisecond; (the variable can be in Chinese) 1.9 display Chinese date -- year month day time string strY = currentTime. toString (f); Do not display second 1.10 get Chinese Date display _ year and month string strYM = currentTime. toString (y); 1.11 returns the Chinese Date display _ month-day string strMD = currentTime. toString (m); 1.12 The current year, month, and day. Format: 2003-9-23string strYMD = currentTime. toString (d); 1.13 returns the current time, in the format of 14: 24 string strT = currentTime. toString (t );

 

2. Convert Int32.Parse (variable) Int32.Parse (constant) into a 32-digit font

 

3. variables. toString () 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.20.12345.tostring (x); generate 3039 (hexadecimal) 12345. toString (p); generate 1,234,500.00%

 

4. Variable. Length numeric string Length: for example, string str = China; int Len = str. Length; Len is a custom variable, and str is the variable name of the string to be tested.

 

5. System. text. encoding. default. convert the GetBytes (variable) code to a bit Code, for example, byte [] bytStr = System. text. encoding. default. getBytes (str); then we can get the bit length: len = bytStr. length;

 

6. The System. Text. StringBuilder () string is added, and the (+ is the same ?) For example, System. Text. StringBuilder sb = new System. Text. StringBuilder (); sb. Append (China); sb. Append (people); sb. Append (Republic of China );

 

7. Variable. Substring (parameter 1, parameter 2); a bit similar to the Left, right, and Mid functions in vb. Part of the string to be truncated. Parameter 1 is the start number of digits on the left, and parameter 2 is the number of digits to be truncated. For example, string s1 = str. Substring (); parameter 2 can be set to the default value, indicating that the string end is obtained from parameter 1. For example, String a = abc; a. substring (1) is bc.

 

8. String user_IP = Request. ServerVariables [REMOTE_ADDR]. ToString (); obtain the remote user IP address.

 

9. Obtain the real IP address of the remote user through the proxy server: if (Request. ServerVariables [HTTP_VIA]! = Null) {string user_IP = Request. ServerVariables [HTTP_X_FORWARDED_FOR]. ToString ();} else {string user_IP = Request. ServerVariables [REMOTE_ADDR]. ToString ();}

 

10. Session [variable]; Access Session value; for example, Value assignment: Session [username] =; Value: Object objName = Session [username]; String strName = objName. toString (); clear: Session. removeAll ();

 

11. String str = Request. QueryString [variable]; Use hyperlinks to send variables. For example, create a hyperlink a href = Edit. aspxfbid = 23 on any page. Click a on the Edit. aspx page and set the value to String str = Request. QueryString [fdid].

 

12. DOC object. CreateElement (new node name); create XML document new node

 

13. parent node. AppendChild (child node); Add the new child node to the parent node of the XML document
14. parent node. RemoveChild (node );
Delete a node

 

15. ResponseResponse. Write (string); Response. Write (variable); output to the page. Response. Redirect (URL address); jump to the page specified by the URL

 

16. char. isWhiteSpce (string variable, number of digits) -- check whether the specified position is empty in logical mode; for example, string str = Chinese people; Response. write (char. isWhiteSpace (str, 2); the result is: True. The first character is 0, and 2 is the third character.

 

17. char. IsPunctuation ('characters') -- check whether the logical character is A punctuation such as Response. Write (char. IsPunctuation ('A'); Return: False

 

18. (int) the character is converted into a number. Check the code. Note that it is a single quotation mark. For example: Response. Write (int) '); the result is a code of the Chinese character: 2001319, (char) code to convert the number into a character, check the characters represented by the Code. For example: Response. Write (char) 22269); returns the country character.

 

20. Trim () clears spaces before and after the string

 

21. string variable. replace (substring, Replace with) string replacement, for example, string str = China; str = str. replace (national, central); Replace the national character with the central character Response. write (str); the output result is central and then as follows: (this is very practical) string str = This is a script; str = str. replace (, fontfont); Replace the left angle brackets with font and font (or Replace with, but it is estimated that after the XML is saved, it will still be restored) Response. write (str);: If this script is not replaced, it will not be displayed. If it is a script, it will be run. After replacement, the script will not run. The value of this Code is that you can invalidate and display all HTML tags in a text file to protect your interactive websites. Specific implementation: Add the following code to your form submission button script: string strSubmit = label1.Text; label1 is the Control ID for you to submit data. StrSubmit = strSubmit. Replace (, fontfont); then save or output strSubmit. This method can also be used to easily implement the UBB code.

 

22. The maximum values of Math. Max (I, j) in I and j, for example, int x = Math. Max (5, 10). x takes the value 10.

 

23. string comparison generally uses if (str1 = str2) {}, but there are other methods (1), string str1; str2 syntax str1.EndsWith (str2 ); _ check whether str1 ends with str2 and returns a Boolean value. for example, if (str1.EndsWith (str2) {Response. write (string str1 ends with + str2 +);} (2), syntax str1.Equals (str2); _ checks whether string str1 is equal to string str2 and returns a Boolean value, the usage is the same as above. (3) syntax Equals (str1, str2); _ checks whether str1 is equal to str2 and returns a Boolean value. The usage is the same as above.

 

24. IndexOf () and LastIndexOf () are used to search for the position of the specified character or string that appears for the first time (the last time). The index value, for example, str1.IndexOf (Word), is returned ); search for the index value (position) str1.IndexOf (string) in str1; search for the index value (position) str1.IndexOf (string,) in str1 ); starting from str1 4th characters, search for 2 Characters and the index value (location) of the first character of the string in str1)

 

25. Insert () specifies the index bit in the string to Insert the specified character. For example, str1.Insert (1, word); insert a word at the second character of str1. If str1 is set to China, it is inserted as a Chinese character;

 

26. When PadLeft () and PadRight () are added to the left (or right) of the string with spaces or specified char characters, the string reaches the specified length, for example, % string str1 = Chinese; str1 = str1.PadLeft (10, '1'); no second parameter is added with space Response. write (str1); the result is 1111111 Chinese, and the string length is 10%

 

27. Remove (): Generally, if (str1 = str2) {} is used to delete a specified number of character strings from a specified position. However, there are other methods: 1. string str1; str2 syntax str1.EndsWith (str2); _ checks whether str1 ends with str2 and returns a Boolean value. for example, if (str1.EndsWith (str2) {Response. write (string str1 ends with + str2 +);} 2. Syntax str1.Equals (str2); _ checks whether string str1 is equal to string str2 and returns a Boolean value. The usage is the same as above. 3. Syntax Equals (str1, str2); _ checks whether str1 is equal to str2 and returns a Boolean value.

 

28. Reverse the order of elements in the entire one-dimensional Array. Har [] charArray = "abcde". ToCharArray (); Array. Reverse (charArray); Console. WriteLine (new string (charArray ));

 

29. Determine whether the nth character in a string is capital string str = "abcEEDddd"; Response. Write (Char. IsUpper (str, 3 ));

 

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.