C#8 the operation of a commonly used string

Source: Internet
Author: User

1. Split intercept according to a single separator character

String st= "Gt123_1"; string[] Sarray=st.split ('_');

Output: sarray[0]= "GT123", sarray[1]= "1".

2. Use multiple characters to separate strings

String str = "gtazb_jiangjben_123"; string[] Sarray = str. Split (new char[2] {' J ', ' _ '}); foreach (string E in Sarray) {    Console.WriteLine (e);}

Output: sarray[0]= "Gtazb", sarray[1]= "Jiang", sarray[2]= "Ben", sarray[3]= "123".

3. To intercept a string based on a string or string group

String str = "gtazb_jiangjben_123"; string[] Sarray = str. Split (New string[]{"Ji", "JB"}, Stringsplitoptions.removeemptyentries); foreach (string E in Sarray) {    Console.WriteLine (e);}

Output: sarray[0]= "Gtazb_", sarray[1]= "Ang", sarray[2]= "en_123".

4. Extract the string with the first character of the string starting with the length J;

String str = "gtazb_jiangjben_123"; int start=3,length=8; Console.WriteLine (str. Substring (start-1, length));

Output: Azb_jian.

5. Extracting the string with the right-length I in the string

String str = "gtazb_jiangjben_123"; string tst;int i=5;tst = str. Substring (str. LENGTH-I); Console.WriteLine (tSt);

Output: n_123.

6. Replacing a specific string in a string

String str = "gtazb_jiangjben_123"; string tst;tst = str. Replace ("123", "321"); Console.WriteLine (tSt);

Output: gtazb_jiangjben_321.

7. Delete a specific string in the string jiangj

String str = "gtazb_jiangjben_123"; string tst;tst = str. Replace ("Jiangj", "" "); Console.WriteLine (tSt);

Output: gtazb_ben_123.

8. Delete the string of the specified length (length) for the specified position (i)

String str = "gtazb_jiangjben_123"; int i=5,length=8;str=str.remove (i,length); Console.WriteLine (str);

Output: gtazben_123.

C#8 the operation of a commonly used string

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.