) C #. net Common string functions

Source: Internet
Author: User
Tags servervariables truncated

Compare compares the content of strings and determines whether some characters are equal based on the cultural background (location ).

CompareOrdinal is the same as Compare, but does not consider the cultural background.

Format: a string containing various values and a description of how to Format each value

IndexOf specifies the first occurrence of a given substring or character in a string.

IndexOfAny specifies the first occurrence of a character or a group of characters in a string.

LastIndexOf is the same as IndexOf, but located at the last location

LastIndexOfAny and IndexOfAny, but locate the last location

At the beginning of the string, PadLeft fills the string with the specified repeated characters

At the end of the string, PadRight fills the string with the specified repeated characters

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

1. DateTime numeric type
System. DateTime currentTime = new System. DateTime ();
1.1 take the current year, month, day, hour, minute, second
CurrentTime = System. DateTime. Now;
1.2 get current year
Int Year = currentTime. Year;
1.3 take the current month
Int Month = currentTime. Month;
1.4 get the current day
Int Day = currentTime. Day;
1.5 current time
Int = currentTime. Hour;
1.6 get the current score
Int = currentTime. Minute;
1.7 takes the current second
Int Second = currentTime. Second;
1.8 takes the current millisecond
Int Millisecond = currentTime. Millisecond;
(The variable can be in Chinese)
2. Int32.Parse (variable) Int32.Parse ("constant ")
Convert string to 32-digit font

3. Variable. ToString ()
Convert string type
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 type
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. GetBytes (variable)
Convert character code to 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 as the + number ?)
For example, System. Text. StringBuilder sb = new System. Text. StringBuilder ("");
Sb. Append ("China ");
Sb. Append ("people ");
Sb. Append ("Republic ");

7. Variable. Substring (parameter 1, parameter 2 );
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 );

8. String user_IP = Request. ServerVariables ["REMOTE_ADDR"]. ToString ();
Obtain the IP address of a 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, assign a value: Session ["username"] = "";

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

11. String str = Request. QueryString ["variable"];
Use hyperlinks to send variables.
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 node for the XML document

13. parent node. AppendChild (child node );
Add the 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 ");
Jump to the page specified by the URL

16. char. IsWhiteSpce (STRING variable, digits) -- Logical type
Check whether the specified position contains null characters;
For example:
String str = "Chinese people ";
Response. Write (char. IsWhiteSpace (str, 2); // The result is True. The first character is 0, and the second is the third character.

17. char. IsPunctuation ('characters') -- Logical type
Check whether a character is a punctuation mark.
For example: Response. Write (char. IsPunctuation ('A'); // return: False

18. (int) 'characters'
Convert the character into a number and check the code. Note that it is a single quotation mark.
For example:
Response. Write (int) 'in'); // The code with the result of a Chinese character: 20013

19. (char) Code
Convert the number into characters and check the characters represented by the Code.
For example:
Response. Write (char) 22269); // return the word "country.

20. Trim ()
Clear leading and trailing Spaces

21. string variable. Replace ("substring", "Replace ")
String replacement
For example:
String str = "China ";
Str = str. Replace ("country", "Central"); // 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: "This is a <script> script"

If this parameter is not replaced, <script> is not displayed. If it is a script, it is run. If it is replaced, the script is 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 ("<", "<font> </font> ");
Then save or output strSubmit.
This method can also be used to easily implement the UBB code.

22. Math. Max (I, j)
Take the maximum value in I and j.
For example, int x = Math. Max (5, 10); // x, the value 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 ("string str1 ends with" + str2 + ");}

(2 ),
// Syntax: str1.Equals (str2); _ checks whether str1 is equal to str2 and returns a Boolean value.

(3 ),
// Syntax Equals (str1, str2); _ checks whether str1 is equal to str2 and returns a Boolean value.

24. IndexOf (), LastIndexOf ()
Search for the position where the specified character or string appears for the first time (the last time) in the 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 "string" in str1)
Str1.IndexOf ("string", 4th); // query the index value (location) of the first character of the "string" in str1 for 2 characters starting from str1 characters)

25. Insert ()
Insert the specified character into the specified index position in the string. For example:
Str1.Insert (1, "word"); Insert "word" to the second character of str1. If str1 = "China", it is inserted as "Chinese country ";

26. PadLeft (), PadRight ()
Add a space or a specified char character to the left (or right) of the string so that 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 ()
Deletes a specified number of characters from a specified position.
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 ("string str1 ends with" + str2 + ");}

2,
// Syntax: str1.Equals (str2); _ checks whether str1 is equal to str2 and returns a Boolean value.

3,
// Syntax Equals (str1, str2); _ checks whether str1 is equal to str2 and returns a Boolean value.

IndexOf ()
Searches for the position of the specified character or string that appears for the first time in the string, and returns the first 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 of the first character of "string" in str1 (BIT)
Str1.IndexOf ("string", 4th); // starting from str1 characters, search for 2 characters, search for the index value (location) of the first character of the "string" in str1)

1.9 display Chinese date -- year, month, and day
String strY = currentTime. ToString ("f"); // seconds not displayed

1.10 use Chinese date to display _ year and month
String strYM = currentTime. ToString ("y ");

1.11 retrieve Chinese Date display _ month/day
String strMD = currentTime. ToString ("m ");

1.12 take the current year, month, and day in the format of 2003-9-23
String strYMD = currentTime. ToString ("d ");

1.13 get the current time, format: 14: 24
String strT = currentTime. ToString ("t ");
Update:

C #. net functions and method sets (add them together)

1. DateTime numeric type
System. DateTime currentTime = new System. DateTime ();
1.1 take the current year, month, day, hour, minute, second
CurrentTime = System. DateTime. Now;
1.2 get current year
Int Year = currentTime. Year;
1.3 take the current month
Int Month = currentTime. Month;
1.4 get the current day
Int Day = currentTime. Day;
1.5 current time
Int = currentTime. Hour;
1.6 get the current score
Int = currentTime. Minute;
1.7 takes the current second
Int Second = 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, and day
String strY = currentTime. ToString ("f"); // seconds not displayed

1.10 use Chinese date to display _ year and month
String strYM = currentTime. ToString ("y ");

1.11 retrieve Chinese Date display _ month/day
String strMD = currentTime. ToString ("m ");

1.12 Chinese year, month, and day
String strYMD = currentTime. ToString ("D ");

1.13 get the current time, format: 14: 24
String strT = currentTime. ToString ("t ");

1.14 obtain the current time in the format of 2003-09-23T14: 46: 48
String strT = currentTime. ToString ("s ");

1.15 obtain the current time in the format of 2003-09-23 14: 48: 30Z
String strT = currentTime. ToString ("u ");

1.16 The current time. format:
String strT = currentTime. ToString ("g ");

1.17 take the current time, format: Tue, 23 Sep 2003 14:52:40 GMT
String strT = currentTime. ToString ("r ");

1.18 obtain the date and time of the current time n days later
DateTime newDay = DateTime. Now. AddDays (100 );

2. Int32.Parse (variable) Int32.Parse ("constant ")
Convert string to 32-digit font

3. Variable. ToString ()
Convert string 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 type
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. GetBytes (variable)
Convert character code to 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 as the + number ?)
For example, System. Text. StringBuilder sb = new System. Text. StringBuilder ("");
Sb. Append ("China ");
Sb. Append ("people ");
Sb. Append ("Republic ");

7. Variable. Substring (parameter 1, parameter 2 );
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 );

8. String user_IP = Request. ServerVariables ["REMOTE_ADDR"]. ToString ();
Obtain the IP address of a 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, assign a value: Session ["username"] = "";

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

11. String str = Request. QueryString ["variable"];
Use hyperlinks to send variables.
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 node for the XML document

13. parent node. AppendChild (child node );
Add the 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 ");
Jump to the page specified by the URL

16. char. IsWhiteSpce (STRING variable, digits) -- Logical type
Check whether the specified position contains null characters;
For example:
String str = "Chinese people ";
Response. Write (char. IsWhiteSpace (str, 2); // The result is True. The first character is 0, and the second is the third character.

17. char. IsPunctuation ('characters') -- Logical type
Check whether a character is a punctuation mark.
For example: Response. Write (char. IsPunctuation ('A'); // return: False

18. (int) 'characters'
Convert the character into a number and check the code. Note that it is a single quotation mark.
For example:
Response. Write (int) 'in'); // The code with the result of a Chinese character: 20013

19. (char) Code
Convert the number into characters and check the characters represented by the Code.
For example:
Response. Write (char) 22269); // return the word "country.

20. Trim ()
Clear leading and trailing Spaces

21. string variable. Replace ("substring", "Replace ")
String replacement
For example:
String str = "China ";
Str = str. Replace ("country", "Central"); // 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: "This is a <script> script"

If this parameter is not replaced, <script> is not displayed. If it is a script, it is run. If it is replaced, the script is 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 ("<", "<font> </font> ");
Then save or output strSubmit.
This method can also be used to easily implement the UBB code.

22. Math. Max (I, j)
Take the maximum value in I and j.
For example, int x = Math. Max (5, 10); // x, the value 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 ("string str1 ends with" + str2 + ");}

(2 ),
// Syntax: str1.Equals (str2); _ checks whether str1 is equal to str2 and returns a Boolean value.

(3 ),
// Syntax Equals (str1, str2); _ checks whether str1 is equal to str2 and returns a Boolean value.

24. IndexOf (), LastIndexOf ()
Search for the position where the specified character or string appears for the first time (the last time) in the 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 "string" in str1)
Str1.IndexOf ("string", 4th); // query the index value (location) of the first character of the "string" in str1 for 2 characters starting from str1 characters)

25. Insert ()
Insert the specified character into the specified index position in the string. For example:
Str1.Insert (1, "word"); Insert "word" to the second character of str1. If str1 = "China", it is inserted as "Chinese country ";

26. PadLeft (), PadRight ()
Add a space or a specified char character to the left (or right) of the string so that 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 ()
Deletes a specified number of characters from a 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.