C # common functions and method sets)

Source: Internet
Author: User
Tags servervariables
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 seconds 1.10 use Chinese date to display _ year and month string strym = currenttime. tostring ( "Y" ); 1.11 use the Chinese Date display _ month-day string strmd = currenttime. tostring ( "M" ); 1.12 use the Chinese year, month, and day string strymd = currenttime. tostring ( "D" ); 1.13 takes the current time, in the format of 14: 24 string strt = currenttime. tostring ( "T" ); 1.14 takes the current time in the format of 2003-09-23t14: 46: 48 string strt = currenttime. tostring ( "S" ); 1.15 takes the current time in the format of 2003-09-23 14: 48: 30 zstring strt = currenttime. tostring ( "U" ); 1.16 takes the current time in the format of 2003-09-23 14: 48 string strt = currenttime. tostring ( "G" ); 1.17 takes the current time, in the format of Tue, 23 Sep 2003 14:52:40 gmtstring strt = currenttime. tostring ( "R" ); 1.18 obtain the datetime newday = datetime. Now. adddays (100) of the current time after N days; 2. int32.parse (variable) int32.parse ( "Constant" ) Convert string type to 32-digit font. 3. Convert variable. tostring () string type to string 12345. tostring ("N" ); // Generate 12,345.00 12345. tostring ( "C" ); // Generate ¥12,345.00. 12345. tostring ( "E" ); // Generate 1.234500e + 004 12345. tostring ( "F4" ); // Generate 12345.0000 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. system. text. stringbuilder ( "" ) String addition. (is the same with the + sign ?) For example, system. Text. stringbuilder sb = new system. Text. stringbuilder ( "" ); Sb. append ( "China" ); Sb. append ( "People" ); Sb. append ("Republic" ); 7. variables. substring (parameter 1, parameter 2); part of the truncated string. Parameter 1 is the left starting digit, and parameter 2 is the number of truncated digits. for example, string S1 = Str. substring (0, 2); 8, string user_ip = request. servervariables [ "Remote_addr" ]. Tostring (); obtain the IP address of the remote user 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" ] ="Bush" ; Value: Object objname = session [ "Username" ]; String strname = objname. tostring (); clear: Session. removeall (); 11, string STR = request. querystring [ "Variable" ]; Send variables with hyperlinks. For example, create a hyperlink on any page: <a href = edit. aspx? Fbid = 23> click </a>. On the edit. ASPX page, set the value to string STR = request. querystring [ "Fdid" ]; 12. Doc object. createelement ( "New node name" ); Create a New XML file node 13 and a parent node. appendchild (child node); Add the new child node to the XML document parent node 14. parent node. removechild (node); delete node 15 and responseresponse. write ( "String" ); Response. Write (variable); output to the page. response. Redirect ( "Url address" ); Jump to the page 16 specified by the URL, Char. iswhitespce (STRING variable, number of digits) -- check whether the specified position is empty in logic; for example: String STR = "Chinese people" ; Response. Write (char. iswhitespace (STR, 2 )); // The result is true. The first character is 0, and the second character is the third character. 17. Char. ispunctuation ('characters') -- check whether the logical character is a punctuation such as response. Write (char. ispunctuation ('A ')); // Return value: false 18. (INT) 'char' converts the character into a number for query. Code Point, note that it is single quotes. For example, in response. Write (INT '); // Code with a result of the Chinese character: 20013 19. The (char) code converts a number into a character and queries the characters represented by the Code, for example, response. Write (char) 22269 ); // Return the word "country. 20. Trim () clears the leading and trailing spaces of the string 21 and string variable. Replace ( "Sub-string" ,"Replace" ) String replacement, for example, string STR = "China" ; STR = Str. Replace ( "Country" , "Yang" ); // Replace the Chinese character with the central character Response. Write (STR ); // The output result is "Central" Another example is: (this is very practical) string STR = "This is a <SCRIPT> script" ; STR = Str. Replace ( "<" , "<Font> </font>" ); // Replace the left angle brackets with <font> and </font> (or replace them with <, but it is estimated that after saving the XML, it will be restored again) Response. Write (STR ); // Display as follows: "This is a <SCRIPT> script" If not replaced, <SCRIPT> is not displayed. If it is a script, it runs. If it is replaced, the script does not run. the value of this Code is that you can invalidate and display all HTML tags in a text. specific implementation: Add the following code to your form submission button script: String strsubmit = label1.text; // Label1 is the ID of the control that allows the user to submit data. Strsubmit = strsubmit. Replace ( "<" , "<Font> </font>" ); Then save or output strsubmit. this method can also be used to easily implement the UBB code. 22. Math. max (I, j) obtains the maximum values in I and j, for example, int x = math. max (5, 10 ); // The value of X is 10. 23. String comparison is generally used: 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 ( "Str1 is" + Str2 + "Ended" );} (2 ), // Syntax: str1.equals (str2); _ check whether str1 is equal to str2. Return a Boolean value, which is the same as the preceding syntax. (3 ), // Syntax equals (str1, str2); _ checks whether str1 is equal to str2 and returns a Boolean value. 24. indexof () and lastindexof () are used to locate the first (last) occurrence of a specified character or string in a string, and return the index value, for example, str1.indexof ( "Word" ); // Search for the index value (location) of the word in str1) Str1.indexof ( "String" ); // Search for the index value (location) of the first character of the "string" in str1) Str1.indexof ("String" , 3, 2 ); // Search for 2 characters starting from str1 4th characters and the index value (location) of the first character of "string" in str1) 25. insert () specifies the index bit in the string to insert the specified character. For example, str1.insert (1, "Word" ); Insert "word" to the second character of str1, if str1 = "China" After the string is inserted, it is "Chinese". 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 () Delete the specified number of characters from the specified position <% string str1 = "I am one of Saddam's fans" ; Response. Write (str1.remove (5, 4 )); // The result is "I am one of Saddam" %>

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.