C # string functions based on Programming

Source: Internet
Author: User

C # common string functions

Example 1:

Returns the string's case-insensitive function.

Tolower (): returns the string in lowercase.

Toupper (): returns the string in uppercase.

Note:

The string is immutable, so these functions do not directly change the content of the string, but return the modified string in the form of the function return value.

The source code is as follows:

Using system; using system. collections. generic; using system. text; namespace string function learning {class program {static void main (string [] ARGs) {string S = "good"; S = S. tolower (); // s. tolower (): the return value is the lower-case console of the string. writeline (s);/* instead of changing the content of the string, it generates a new string that is changed to lowercase, and points it to the new string with S. */Console. writeline (S. toupper (); // S. toupper () returns an uppercase string. Console. readkey ();}}}

Run:

Example 2:

String to remove the blank functions on both sides

Trim ();

The source code is as follows:

Using system; using system. collections. generic; using system. text; namespace string function learning {class program {static void main (string [] ARGs) {string S = "good"; console. writeline ("before using the blank function on both sides: \ n | {0} |", S); // The blank string on both sides of the call function S = S. trim (); console. writeline ("after the blank function on both sides is used: \ n | {0} |", S); // After the function is called, there is no blank console on both sides of the string. readkey ();}}}

Program:

Example 3:

Case-insensitive string comparison functions

"ABC" = "ABC"

Bool string. Equals (string value, stringcomparsion comparisontype) (+ 2 overload)
Determines whether the string has the same value as the specified system. String object.
The parameter specifies the regional, case-sensitive, and comparative sorting rules.
Exception:
System. nullreferenceexception
System. argumentexception

The source code is as follows:

Using system; using system. collections. generic; using system. text; namespace string function learning {class program {static void main (string [] ARGs) {/************************************** * ******** bool string. equals (string value, stringcomparsion comparisontype) (+ 2 overload) * determines whether the string matches the specified system. the string object has the same value. * The parameter specifies the regional, case-sensitive, and comparative sorting rules. * Exception: * system. nullreferenceexception * system. argumentexception *************************************** * ******** stringcomparsion Enumeration type * bool B = "ABC" = "ABC "; * ignore: Case Sensitive * = is a case sensitive comparison. Equals ("ABC", stringcomparison. ordinalignorecase) is a case-insensitive comparison. * // determine "ABC" = "ABC". The return value of bool type is false. After the function is called, The boolean type is true bool B = "ABC ". equals ("ABC", stringcomparison. ordinalignorecase); console. writeline (B); // because the case-insensitive function is called, the return value of the bool type is true console. readkey ();}}}

Running result:

Example 4:

String segmentation functions:

String [] Split (Params char [] separator ):
Splits a string into a string array according to the specified delimiter

Splits a string into a string array using the specified delimiter:

1. Separate AAA, BBB, CCC, dddfdsajf with commas (,).

The source code is as follows:

Using system; using system. collections. generic; using system. text; namespace string function learning {class program {static void main (string [] ARGs) {string S = "AAA, BBB, CCC, dddfdsajf "; /* string [] Split (Params char [] separator): * Splits the string into a String Array Based on the specified separator */string [] STRs = S. split (','); // splits string s into string arrays according. // Print the content in STRs in a loop String Array foreach (string item in STRs) {console. writeline (item) ;}console. readkey ();}}}

Program:

2. Remove the space separator function from the result.

String [] Split (CHARP [] separator, stringsplitoptions options)
Splits a string into a String Array Based on the specified char.

(Option removes the blank characters in the result when removeemptyentrles is used)

Separate AAA, BBB, CCC, dddfdsajf. If you use the preceding method to separate, a blank character will appear:

This function can be used to avoid the above situations.

Using system; using system. collections. generic; using system. text; namespace string function learning {class program {static void main (string [] ARGs) {string S = "AAA, BBB, CCC, dddfdsajf "; /* string [] Split (CHARP [] separator, stringsplitoptions options) * separates the string into a String Array Based on the specified char ** (when the option is used to retrieve removeemptyentrles, the blank character in the result is removed) */string [] STRs = S. split (New char [] {','}, stringsplitoptions. removeemptyentries); // print the content in the STRs array cyclically (string item in STRs) {console. writeline (item);} console. readkey ();}}}

Program running result:

3. Use a string as a separator to separate string functions:

String [] Split (string [] separator, stringsplitoptions options)
Splits a string into a String Array Based on the specified string separator.

For example, you can separate "I Am a nebula, I am fairy, and I am Yan Ke" into "fairy yake"

The source code is as follows:

Using system; using system. collections. generic; using system. text; namespace string function learning {class program {static void main (string [] ARGs) {string S = "I Am a nebula, I am a fairy, I am a Yan Ke "; /* string [] Split (New String [] separator, stringsplitoptions options) * split the string into a String Array Based on the specified char ** (when the option is used to retrieve removeemptyentrles, the blank character in the result is removed) */string [] STRs = S. split (New String [] {"I am"}, stringsplitoptions. removeemptyentries); // print the content in the STRs array cyclically (string item in STRs) {console. writeline (item);} console. readkey ();}}}

Program running:

Related Article

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.