C # Summary of several methods for intercepting strings,

Source: Internet
Author: User

C # Summary of several methods for intercepting strings,

1. Use split to intercept a single Separator

For example

string st="GT123_1";string[] sArray=st.split("_");

You can get sArray [0] = "GT123", sArray [1] = "1 ";

2. Use multiple characters to separate strings

For example

string str = "GTAZB_JiangjBen_123";string[] sArray = str.Split(new char[2] { 'j', '_' });foreach(string e in sArray){Console.WriteLine(e);}

Get sArray [0] = "GTAZB", sArray [1] = "Jiang", sArray [2] = "Ben", sArray [3] = "123 ";

3. truncate a string based on a string or a string Group

For example

string str = "GTAZB_JiangjBen_123";string[] sArray = str.Split( new string[]{"Ji","jB"}, StringSplitOptions.RemoveEmptyEntries);foreach(string e in sArray){Console.WriteLine(e);}

Get sArray [0] = "GTAZB _", sArray [1] = "ang", sArray [2] = "en_123 ";

4. Extract the string whose length starts from the I character to j;

For example

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

Output AZB_Jian.

5. Extract the string whose right number is I.

string str = "GTAZB_JiangjBen_123";string tSt;int i=5;tSt = str.Substring(str.Length - i); Console.WriteLine(tSt);

Output n_123;

6. Replace the specified string in the string

string str = "GTAZB_JiangjBen_123";string tSt;tSt = str.Replace("123","321");Console.WriteLine(tSt);

Output GTAZB_JiangjBen_321

7. Delete the specified 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) at the specified position (NTH ).

string str = "GTAZB_JiangjBen_123";int i=5,length=8;str=str.remove(i,length);console.writeline(str);

Output GTAZBen_123.

Reprinted from: http://www.jb51.net/article/35376.htm

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.