C # Summary of several methods for intercepting strings

Source: Internet
Author: User

1. Use split to intercept a single Separator

For example

Copy codeThe Code is as follows: 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

Copy codeThe Code is as follows: 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

Copy codeThe Code is as follows: 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

Copy codeThe Code is as follows: 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.

Copy codeThe Code is as follows: 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

Copy codeThe Code is as follows: 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.

Copy codeThe Code is as follows: 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 ).

Copy codeThe Code is as follows: string str = "GTAZB_JiangjBen_123 ";

Int I = 5, length = 8;

Str = str. remove (I, length );

Console. writeline (str );

Output GTAZBen_123.

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.