c # .net list of commonly used functions

1, DateTime numeric
System.DateTime = currentTime new new System.DateTime ();
1.1 take the current date, day, hour
currentTime = System.DateTime.Now;
1.2 in the current taken
int Year currentTime.Year =;
1.3 take the current month
int month currentTime.Month =;
1.4 take the current date
int day currentTime.Day =;
for 1.5 take the current when
int currentTime.Hour time =;
1.6 take the current sub-
int points currentTime.Minute =;
1.7 seconds to get the current
int second currentTime.Second =;
1.8 take current millisecond
int milliseconds currentTime.Millisecond =;
(Chinese variable available)

1.9 taking Chinese date - time division date
string strY = currentTime.ToString ( "f" ); // do not show seconds

1.10 take in Chinese date _ May
String strYM currentTime.ToString = ( "Y");

1.11 _ taking Chinese date month day
string strMD = currentTime.ToString ( "m" );

1.12 take the current date, in the format: 2003-9-23
String strYMD currentTime.ToString = ( "D");

1.13 take the current division in the format: 14: 24
String currentTime.ToString STRT = ( "T");

2, Int32.Parse (variable) the Int32.Parse ( "constant")
character font converted into 32-bit

3, variable .ToString ()
character converted into a string
12345.ToString ( "n"); // generating 12,345.00
12345.ToString ( "C"); // 12,345.00 generating ¥
12345.ToString ( "E"); // 004 generates 1.234500e
12345.ToString ( "F4"); // generates 12345.0000
12345.ToString ( "X "); // generates 3039 (hexadecimal)
12345.ToString (" P "); // generate 1,234,500.00%


4, numeric variable .Length
taken word length:
as: string str =" Chinese ";
int = Len str.length; variable name // Len is custom variables, str seek is measured string

5, System.Text.Encoding.Default.The GetBytes (variable)
word bit code converted into
Such as: byte [] bytStr = System.Text.Encoding.Default.GetBytes ( str);
and available bit length:
len = bytStr.Length;

. 6, the System.Text.StringBuilder ( "")
strings added (No. is not the same)?
such as: System.Text.StringBuilder SB = new new System.Text.StringBuilder ( "");
sb.append ( "China");
sb.append ( "people");
sb.append ( "Republic ");

7, .substring variable (parameter 1, parameter 2);
taken part of the string, the start bits to the left of the parameter 1, parameter 2, taken a few.
Such as: str.Substring String S1 = (0,2);

. 8, the Request.ServerVariables String user_ip = [ "REMOTE_ADDR"] the ToString ();.
Take the remote IP address of the user

9, the user through the remote proxy take real IP address:
IF (the Request.ServerVariables [ "HTTP_VIA"] = null!) {
String = user_ip the Request.ServerVariables [ "HTTP_X_FORWARDED_FOR"] the ToString ();.
} the else {
. = String user_ip the Request.ServerVariables [ "REMOTE_ADDR"] the ToString ();
}

10, Session [ "variable"];
Access Session value;
e.g., assignment: Session [ "username"] = " Bush";

Value: = the objName the Session Object [ "username"];
String objName.ToString strName = ();
Clear: Session.RemoveAll ();

. 11, STR String = Request.QueryString [ "variable"];
hyperlinked variable transmission.
In either built as a hyperlink: <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");
to create a new XML document node

13, the parent node .AppendChild (child nodes);
the XML document is added under the new parent of the child node

14, the parent node .RemoveChild (node);
deleted node

15, the Response
Response.Write ( "string"



Response.Redirect ( "URL address");
jump to the URL specified page

16, char.IsWhiteSpce (string variable, the number of bits) - Logical
check whether the specified location null character;
such as:
String str = "Chinese people" ;
Response.Write (char.IsWhiteSpace (str, 2)); // result: True, the first character is 0, 2 is the third character.

17, char.IsPunctuation ( '' '' character '' '') - Logical
check whether the punctuation character
such as: Response.Write (char.IsPunctuation ( '' ' ' A '' '')); // returns: False

18 is, (int) '' '' 'character'
the character to digital, check the code point, note the single quotation marks.
Such as:
Response.Write ((int) '' '' in '' ''); // code word results: 20013

. 19, (char) code for
the character into a digital, check code representing characters.
Such as:
Response.Write ((char) 22269); // returns "national" character.

20, Trim ()
Clear a space before and after the string

21, the string variable.



str = str.Replace ( "country", "Central"); // change the country's character as a central character
Response.Write (str); // output is "central"

Again :( this very practical)

String str = " this is the <script> script ";
STR = str.replace (" < "," <font> << / font> "); // replace the left angle brackets <font> and <and </ font> (or replaced <, it is estimated that after various XML deposit, will then propose reduction)
Response.Write (str); // read: "this is a <script> script"

if you do not replace, <script> will not be displayed, if This is a script that will run; and after the replacement, the script will not run.
The value of this code is: You can make all the HTML tags in the text of a failure, all displayed to protect your site has interactive.
Specific implementation: submit your form button script to add the following codes:
String = strSubmit Label1.Text; // label1 you allow users to submit data to the control ID.
strSubmit = strSubmit.Replace ( "<", "<font> << / font>");
and then save or output strSubmit.
This method may also be used to achieve a simple UBB code.

22, Math.Max (i, j)
takes the maximum value of i and j
as int x = Math.Max (5,




str1 String; str2
// Syntax: str1.EndsWith (str2); __ detect whether the end of the string str1 string str2, returns a Boolean value, such as:.
IF (str1.EndsWith (str2)) {Response.Write ( "word str1 string is "str2" ends ");}

(2),
// syntax: str1.Equals (str2); __ detection is equal to the string str1 str2 string, it returns a Boolean value, use ibid.

(3 ),
// syntax Equals (str1, str2); __ detection is equal to the string str1 string str2, returns a Boolean value, use ibid.

24-, IndexOf (), LastIndexOf ()
Find string in a specified character or string position for the first time (last time) appears, returns the index, such as:
str1.IndexOf ( "word"); // find the "word" in str1 index value of (position)
str1.IndexOf ( "string"); / / find "string" index value of the first character in str1 (position)
str1.IndexOf ( "string", 3,2); // str1 from the fourth character, 2 characters lookup, to find "string" in the first character index value (position) in str1 to

25, insert ()
specified index bits into the specified character string. Such as:
str1.Insert (. 1, "word"); Insert "word" in the second character str1 if str1 = "Chinese"




string str1 = "Chinese people";
str1 = str1.PadLeft (10, '' ''. 1 '' ''); // no second parameter spaces
Response.Write (str1); // result is "1111111 China people ", the string length of 10
%>

27, the remove ()
character deletes a specified number from the specified position
comparison string are generally used: if (str1 == str2) { }, but there are other methods:

1,
str1 String; str2
// syntax: str1.EndsWith (str2); __ detect whether the end of the string str1 string str2, returns a Boolean value, such as:.
iF (str1.EndsWith (str2)) {Response.Write ( "word str1 string is "str2" ends ");}

2,
@ syntax: str1.Equals (str2); __ detection is equal to the string str1 str2 string, returns a Boolean value, use ibid.

3,
// syntax Equals (str1, str2); __ detection is equal to the string str1 string str2, returns a Boolean value, use ibid.

IndexOf ()
to find the location specified character string or the string is first found, return the first index value, such as:
str1.IndexOf ( "word"); // find the "word" str1 index value (position)
str1.IndexOf ( "string"); // Find the "string" of the first character index value (position in str1)
str1.IndexOf ( "string", 3,2); // str1 from the fourth character, 2 characters lookup, to find an index value of "string" in the first character str1 (position)  Shenzhen surrogate electric 13,802,269,370   surrogate micro 13,802,269,370   Beijing surrogate micro 13,802,269,370

Guess you like

Origin www.cnblogs.com/bbc2020/p/12457249.html