ASP tutorials. NET C # common classes and methods
1, datetime digital type
System.DateTime currenttime=new System.DateTime ();
1.1 to take the current month and the day seconds
Currenttime=system.datetime.now;
1.2 Take as the year before last
int year =currenttime.year;
1.3 Take the current month
int month =currenttime.month;
1.4 Take the current day
int day =currenttime.day;
1.5 Take current time
int when =currenttime.hour;
1.6 Take the current score
int cent =currenttime.minute;
1.7 Take the current second
int seconds =currenttime.second;
1.8 Take the current millisecond
int millisecond =currenttime.millisecond;
(Variables available in Chinese)
1.9 Chinese date display--year and month
String Stry=currenttime.tostring ("F"); Do not show seconds
1.10 Take Chinese Date show _ Years
String strym=currenttime.tostring ("Y");
1.11 Take Chinese date show _ Month Day
String strmd=currenttime.tostring ("M");
1.12 Take the current date, format: 2003-9-23
String strymd=currenttime.tostring ("D");
1.13 Take the current time, the format is: 14:24
String strt=currenttime.tostring ("T");
2, character-type conversion to 32-digit digital type
Int32.Parse (variable) int32.parse ("constant")
3, variable. ToString ()
Character conversion 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 (16 binary)
12345.tostring ("P"); Generate 1,234,500.00%
4, variable. Length Digital type
Length of string:
such as: string str= "China";
int len = str.length; Len is a custom variable, and STR is the variable name for the measured string.
5, codewords conversion to bit code
System.text.encoding.default.getbytes (variable)
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, (+ is not the same?) )
Such as:
System.Text.StringBuilder sb = new System.Text.StringBuilder ("");
Sb.append ("China");
Sb.append ("people");
Sb.append ("Republic");
7, variable. substring (parameter 1, parameter 2);
Intercepts a portion of the string, parameter 1 is the left starting bit, and the parameter 2 is the Intercept number.
such as: string S1 = str.substring (0,2);
8, take the remote User IP address
String user_ip=request.servervariables["REMOTE_ADDR"].tostring ();
9, through the proxy server to take remote user real IP address:
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. Access Session Value
session["variable"];
For example, assign a value:
session["username"]= "little Bush";
Take value:
Object objname=session["username"];
String strname=objname.tostring ();
Empty:
Session.removeall ();
11. Transfer variables with hyperlinks
String str=request.querystring["variable"];
If you are building hyperlinks on any page: Click
Value in edit.aspx page: string str=request.querystring["Fdid"];
12. Create new node of XML document
Doc object. createelement ("new section named");
13, add the new child node under the XML Document parent node
Parent node. appendchild (child node);
14, delete the node
Parent node. removechild (node);
15, to the page output: response
Response.Write ("string");
Response.Write (variable);
Jumps to the page specified by the URL:
Response.Redirect ("URL address");
16, check whether the specified location is empty characters
CHAR.ISWHITESPCE (string variable, number of digits)--logical type;
Such as:
String str= "Chinese people";
Response.Write (Char.iswhitespace (str,2)); The result is: True, the first character is 0 digits, and 2 is the third character.
17, check whether the character is a punctuation mark
Char.ispunctuation (' character ')--logical type
Such as:
Response.Write (Char.ispunctuation (' a ')); Return: False
18, the character into a number, check the code point, note that the single quotation marks.
(int) ' Character '
Such as:
Response.Write ((int) ' ' in '); The result is the code in the word: 20013
19, the number into the character, look up the code represents the character: (char) code
Such as:
Response.Write ((char) 22269); Return to the word "state".
20, clear the string before and after the space: trim ()
21, String replacement
String variable. Replace ("substring", "Replace with")
Such as:
String str= "China";
Str=str.replace ("Country", "central"); Change the country word to the word
Response.Write (str); The output is "central"
Another example: (This very practical)
String Str= "This is
The value of this code is that you can disable all HTML tags in one text, display them all, and protect your interactive site.
Concrete implementation: Add your Form submit button script with the following code:
String Strsubmit=label1.text; Label1 is the ID of the control you want the user to submit data to.
Strsubmit=strsubmit.replace ("A", " ");
Then save or output strsubmit.
You can also use this method to easily implement UBB code.
22, take the maximum value of I and J: Math.max (I,J)
such as int x=math.max (5,10); X will take a value of 10
Add a bit of it. 23. String contrast ...
23, string contrast is generally used: if (STR1==STR2) {}, but there are other ways:
(1),
String str1; str2
Grammar: Str1.endswith (STR2); __ The string str1 whether to end with a string str2 returns a Boolean value. For example:
if (Str1.endswith (str2)) {Response.Write ("string str1 is terminated" by "+str2+"); }
(2),
Grammar: Str1.equals (STR2); __ Detect string str1 is equal to String str2, return boolean value, usage ibid.
(3),
Syntax equals (STR1,STR2); __ Detect string str1 is equal to String str2, return boolean value, usage ibid.
24. Find the position of the first (last) occurrence of a specified character or string in a string, returning the index value: indexof (), LastIndexOf (), such as:
Str1.indexof ("word");//Find the index value (position) of the 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), and/or from the str1 4th character, look for 2 characters, find the index value of the first character of "string" in str1 (position)
25. Inserts the specified character in the string: Insert (), such as:
Str1.insert (1, "word"); in str1 the second word entered prompt inserted "word", if str1= "China", inserted after "the word state";
26, in the string left (or right) plus space or specify char characters, so that the string reached the specified length: PadLeft (), PadRight (), such as:
<%
String str1= "Chinese";
Str1=str1.padleft (10, ' 1 "); No second parameter is plus space
Response.Write (STR1); The result is "1111111 Chinese" with a string length of 10.
%>
27. Delete the specified number of characters from the specified position: remove ()
28. Reverses the order of elements in an entire one-dimensional array.
har[] Chararray = "ABCDE". ToCharArray ();
Array.reverse (Chararray);
Console.WriteLine (New String (Chararray));
29. Determine if the nth character in a string is uppercase
String str= "abceedddd";
Response.Write (Char.isupper (str,3));