Common C # String functions Daquan

Source: Internet
Author: User
Tags servervariables

Compare Compare the contents of a string, consider a cultural background (place), determine whether certain characters are equal

CompareOrdinal and compare, but without regard to cultural background

Format formats a string containing various values and how to format the specifier for each value

IndexOf position of the first occurrence of a given substring or character in a positional string

IndexOfAny position of the first occurrence of a character or set of characters in a positional string

LastIndexOf is the same as indexof, but locates the last occurrence of the position

Lastindexofany and IndexOfAny, but position the last occurrence

PadLeft the string at the beginning of the string by adding the specified repeating character

PadRight the string by adding the specified repeating character to the end of the string

Replace replaces the given character or substring in the string with another character or substring

1, DateTime Digital type
System.DateTime currenttime=new System.DateTime ();
1.1 Minutes and seconds of the current month and day
Currenttime=system.datetime.now;
1.2 Take the year before
int year =currenttime.year;
1.3 Take 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 Taking current points
int sub-=currenttime.minute;
1.7 Take current seconds
int seconds =currenttime.second;
1.8 Take the current millisecond
int milliseconds =currenttime.millisecond;
(Variable available in Chinese)
2, int32.parse (variable) int32.parse ("constant")
Character conversion to 32-bit digital type

3, variables. ToString ()
Character conversion to string
12345.ToString ("n"); Generate 12,345.00
12345.ToString ("C"); Generate ¥12,345.00
12345.ToString ("E"); Build 1.234500e+004
12345.ToString ("F4"); Build 12345.0000
12345.ToString ("X"); Build 3039 (16 binary)
12345.ToString ("P"); Generate 1,234,500.00%


4, variables. Length Digital Type
Fetch string Length:
such as: string str= "China";
int Len = str. Length; Len is a custom variable, and STR is the variable name of the string to be measured.

5. System.Text.Encoding.Default.GetBytes (variable)
Loadline conversion to bit code
such as: byte[] bytstr = System.Text.Encoding.Default.GetBytes (str);
The bit length can then be obtained:
len = bytstr.length;

6, System.Text.StringBuilder ("")
The string is added, (is the + number the same?) )
such as: System.Text.StringBuilder SB = new System.Text.StringBuilder ("");
Sb. Append ("Zhonghua");
Sb. Append ("The People");
Sb. Append ("Republic");

7, variables. Substring (parameter 1, parameter 2);
Intercept a part of the string, parameter 1 is the left start number, parameter 2 is to intercept several.
such as: string S1 = str. Substring (0,2);

8, String user_ip=request.servervariables["REMOTE_ADDR"]. ToString ();
Take the remote User IP address

9, through the proxy server to take the 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, session["variable"];
Access session value;
For example, assignment: session["username"]= "little Bush";

Value: Object objname=session["username"];
String strname=objname.tostring ();
Emptying: Session.removeall ();

11, String str=request.querystring["variable"];
Use hyperlinks to transfer variables.
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 a new XML document node

13, parent node. AppendChild (sub-node);
Add a new child node to the XML document parent node

14, parent node. RemoveChild (node);
Delete a node

15, Response
Response.Write ("string");
Response.Write (variable);
Output to the page.

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

16, Char. ISWHITESPCE (string variable, number of digits)--Logic type
Check if the specified position is null character;
Such as:
String Str= "the 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 ')--Logic type
Whether Chag is a punctuation mark
such as: Response.Write (char. Ispunctuation (' A ')); return: False

18, (int) ' character '
Turn the characters into numbers, check the code points, and note the single quotes.
Such as:
Response.Write ((int) ' Medium '); Code with result for Chinese characters: 20013

19, (char) code
Convert a number to a character and look up the character represented by the code.
Such as:
Response.Write ((char) 22269); Returns the word "country".

20. Trim ()
Clear space before and after string

21, string variables. Replace ("sub-string", "Replace with")
String substitution
Such as:
String str= "China";
Str=str. Replace ("Country", "central"); Change the word of the country to the central word
Response.Write (str); The output is "central"

Another example: (This is very practical)

String Str= "This is the <script> script";
Str=str. Replace ("<", "<font><</font>"); Replace the left angle bracket with <font> < with </font> (or &LT, but estimate that it will be restored after XML has been saved)
Response.Write (str); Display as: "This is <script> script"

If you do not replace,<script> will not be displayed, if it is a script, it will run, and the script will not run when replaced.
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.
Specific 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>");
Then save or export the Strsubmit.
The UBB code can also be implemented simply by using this method.

22, Math.max (I,J)
The maximum value in I and J is taken
such as int x=math.max (5,10); X will take a value of 10

Add a little bit. 23, string comparison
Add a little.

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

(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" ends with "+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 of "word" in str1 (position)
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
String comparisons are generally used: if (STR1==STR2){}, but there are other ways:

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" ends with "+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.

IndexOf ()
Finds the first occurrence of a specified character or string in a string, and returns the index value, such as:
Str1. IndexOf ("word");//Find the index value of "word" in str1 (position)
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)

1.9 Chinese date display--month and day
String Stry=currenttime.tostring ("F"); Do not display seconds

1.10 Chinese Date Display _ Month
String strym=currenttime.tostring ("Y");

1.11 Chinese Date Display _ Month Day
String strmd=currenttime.tostring ("M");

1.12 The current month date, in the format: 2003-9-23
String strymd=currenttime.tostring ("D");

1.13 Take the current time, in the format: 14:24
String strt=currenttime.tostring ("T");
Update, cannot edit:

C#.net function and method set (everyone to add AH)


1, DateTime Digital type
System.DateTime currenttime=new System.DateTime ();
1.1 Minutes and seconds of the current month and day
Currenttime=system.datetime.now;
1.2 Take the year before
int year =currenttime.year;
1.3 Take 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 Taking current points
int sub-=currenttime.minute;
1.7 Take current seconds
int seconds =currenttime.second;
1.8 Take the current millisecond
int milliseconds =currenttime.millisecond;
(Variable available in Chinese)

1.9 Chinese date display--month and day
String Stry=currenttime.tostring ("F"); Do not display seconds

1.10 Chinese Date Display _ Month
String strym=currenttime.tostring ("Y");

1.11 Chinese Date Display _ Month Day
String strmd=currenttime.tostring ("M");

1.12 Chinese Month Day
String strymd=currenttime.tostring ("D");

1.13 Take the current time, in the format: 14:24
String strt=currenttime.tostring ("T");

1.14 Take the current time, in the format: 2003-09-23t14:46:48
String strt=currenttime.tostring ("s");

1.15 Take the current time, in the format: 2003-09-23 14:48:30z
String Strt=currenttime.tostring ("U");

1.16 Take the current time, in the format: 2003-09-23 14:48
String strt=currenttime.tostring ("G");

1.17 Take the current time, in the format: Tue, SEP 2003 14:52:40 GMT
String strt=currenttime.tostring ("R");

1.18 Date time to get the current time n days
DateTime newday = DateTime.Now.AddDays (100);

2, int32.parse (variable) int32.parse ("constant")
Character conversion to 32-bit digital type

3, variables. ToString ()
Character conversion to string
12345.ToString ("n"); Generate 12,345.00
12345.ToString ("C"); Generate ¥12,345.00
12345.ToString ("E"); Build 1.234500e+004
12345.ToString ("F4"); Build 12345.0000
12345.ToString ("X"); Build 3039 (16 binary)
12345.ToString ("P"); Generate 1,234,500.00%


4, variables. Length Digital Type
Fetch string Length:
such as: string str= "China";
int Len = str. Length; Len is a custom variable, and STR is the variable name of the string to be measured.

5. System.Text.Encoding.Default.GetBytes (variable)
Loadline conversion to bit code
such as: byte[] bytstr = System.Text.Encoding.Default.GetBytes (str);
The bit length can then be obtained:
len = bytstr.length;

6, System.Text.StringBuilder ("")
The string is added, (is the + number the same?) )
such as: System.Text.StringBuilder SB = new System.Text.StringBuilder ("");
Sb. Append ("Zhonghua");
Sb. Append ("The People");
Sb. Append ("Republic");

7, variables. Substring (parameter 1, parameter 2);
Intercept a part of the string, parameter 1 is the left start number, parameter 2 is to intercept several.
such as: string S1 = str. Substring (0,2);

8, String user_ip=request.servervariables["REMOTE_ADDR"]. ToString ();
Take the remote User IP address

9, through the proxy server to take the 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, session["variable"];
Access session value;
For example, assignment: session["username"]= "little Bush";

Value: Object objname=session["username"];
String strname=objname.tostring ();
Emptying: Session.removeall ();

11, String str=request.querystring["variable"];
Use hyperlinks to transfer variables.
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 a new XML document node

13, parent node. AppendChild (sub-node);
Add a new child node to the XML document parent node

14, parent node. RemoveChild (node);
Delete a node

15, Response
Response.Write ("string");
Response.Write (variable);
Output to the page.

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

16, Char. ISWHITESPCE (string variable, number of digits)--Logic type
Check if the specified position is null character;
Such as:
String Str= "the 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 ')--Logic type
Whether Chag is a punctuation mark
such as: Response.Write (char. Ispunctuation (' A ')); return: False

18, (int) ' character '
Turn the characters into numbers, check the code points, and note the single quotes.
Such as:
Response.Write ((int) ' Medium '); Code with result for Chinese characters: 20013

19, (char) code
Convert a number to a character and look up the character represented by the code.
Such as:
Response.Write ((char) 22269); Returns the word "country".

20. Trim ()
Clear space before and after string

21, string variables. Replace ("sub-string", "Replace with")
String substitution
Such as:
String str= "China";
Str=str. Replace ("Country", "central"); Change the word of the country to the central word
Response.Write (str); The output is "central"

Another example: (This is very practical)

String Str= "This is the <script> script";
Str=str. Replace ("<", "<font><</font>"); Replace the left angle bracket with <font> < with </font> (or &LT, but estimate that it will be restored after XML has been saved)
Response.Write (str); Display as: "This is <script> script"

If you do not replace,<script> will not be displayed, if it is a script, it will run, and the script will not run when replaced.
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.
Specific 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>");
Then save or export the Strsubmit.
The UBB code can also be implemented simply by using this method.

22, Math.max (I,J)
The maximum value in I and J is taken
such as int x=math.max (5,10); X will take a value of 10

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

(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" ends with "+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 of "word" in str1 (position)
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
<%
String str1= "I am one of Saddam's admirers";
Response.Write (str1. Remove (5,4)); The result is "I'm one of Saddam Hussein."
%>

Source: http://www.cnblogs.com/meil/archive/2011/06/22/2086808.html

Common C # String functions Daquan

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.