SoFunction
Updated on 2025-03-07

Summary of several string interception methods has been sorted out in C#

1. (Substring); (Remove); (Replace)

1. Take the first i characters of the string

(1)string str1=(0,i);
(2)string str1=(i,-i);

2. Remove the first i characters of the string

string str1=(0,i);
string str1=(i);

3. Start taking i characters from the right

string str1=(-i);
string str1=(0,-i);

4. Remove i characters from the right

string str1=(0,-i);
string str1=(-i,i);

5. If there is "abc" in the string, replace it with "ABC"

str=("abc","ABC");

6. The problem of c# intercepting the last character of the string

((",")+1);

7. C# intercepts the last character of the string

k = (-1, 1);

2. Split

1. Use split to intercept according to a single separator character

string st="GT123_1";
string[] sArray=("_");
You can get itsArray[0]="GT123",sArray[1]="1";

2. Use multiple characters to separate strings

For example

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

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

3. Intercept strings based on strings or string groups

For example

string str = "GTAZB_JiangjBen_123";
string[] sArray = ( new string[]{"Ji","jB"}, );
foreach(string e in sArray)
{
(e);
}

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

4. Extract a string with a length j starting from the i-th character in the string;

For example

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

The output is obtained by AZB_Jian.

5. Extract the string with the right number length i in the string

string str = "GTAZB_JiangjBen_123";
string tSt;
int i=5;
tSt = ( - i); 
(tSt);

Output n_123;

6. Replace specific strings in strings

string str = "GTAZB_JiangjBen_123";
string tSt;
tSt = ("123","321");
(tSt);

Output GTAZB_JiangjBen_321

7. Delete specific string Jiangj in string

string str = "GTAZB_JiangjBen_123";
string tSt;
tSt = ("Jiangj","");
(tSt);

Output GTAZB_Ben_123

8. Delete strings of specified length (length) at the specified position (ith)

string str = "GTAZB_JiangjBen_123";
int i=5,length=8;
str=(i,length);
(str);

Output GTAZBen_123.

This is the end of this article about C# that has sorted out several string interception methods. For more related C# string interception content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!