C # Intercept string

Source: Internet
Author: User

1. Split intercept based on a single delimiter character

For example


String st= "Gt123_1";

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


Can get sarray[0]= "GT123", sarray[1]= "1";

2. Separating strings with multiple characters

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 to intercept a string based on a string or 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 extracts the string of the first character of the string beginning with the length of J;

For example


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


The output gets 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 a string jiangj

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


Output gtazb_ben_123

8 Delete A 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);

C # Intercept string

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.