SoFunction
Updated on 2025-03-08

How to split strings in C#

How to split strings in C#

In C#, you can use the () method to split a string.

The method is used in the form:

string[] result = (separatorChars, StringSplitOptions);

The separatorChars parameter is a character array that specifies the delimiter; the StringSplitOptions parameter is an enumeration value that specifies whether to delete blank items in the result array.

For example, the following code separates a string by spaces and outputs the second element in the result array:

string originalString = "Hello world"; 
string[] result = (new char[] { ' ' }, );
 (result[1]); 
// Output "world"

It should be noted that the Split() method returns an array of strings, and elements can be accessed through indexes.

In the above example, since the original string is separated by spaces, there are only two elements in the result array, the second element can be accessed by result[1] .

Of course, the Split() method also supports multiple delimiters, and you can just place multiple delimiters in a character array.

For example, the following code separates a string by two separators:

string originalString = "apple, banana, orange"; 
string[] result = (new char[] { ',', ' ' }, ); 
foreach (string s in result)
 {
     (s);
 } 
// Output: // apple // banana // orange

In the above example, the two separators of comma and space are placed in a character array, and then the original string is separated by the Split() method to obtain an array of strings containing three elements.

Finally, the result array is traversed through the foreach loop and the elements are output one by one.

Several C# string interception methods

Common methods for C# screenshot strings include split, Substring, Replace, remove, etc.

The best one is personally considered to be the split method

Here are the methods above

1. split

The returned string array contains the substrings in this instance (separated by the elements that specify the Unicode character array).

Use split to intercept according to a single separator.

For example, copy the code as follows:

  string st="GT123_1"; 

    string[] sArray=(‘_');// It must be a single quotation
    You can get itsArray[0]="GT123",sArray[1]="1"; 

-Use multiple characters to separate strings

For example Copy the code as follows: 

 string str = "GTAZB_JiangjBen_123"; 

    string[] sArray = (new char[2] { 'j', '_' }); 

    foreach(string e in sArray) 

    { (e); } 

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

-Split( String (), StringSplitOptions)

The returned string array contains the substrings in this string (separated by the elements of the specified string array).

The parameter specifies whether to return an empty array element.

RemoveEmptyEntries means that the empty array element in the returned array is to be omitted

Intercept strings based on strings or string groups

For example, copy the code as follows:

 string str = "GTAZB_JiangjBen_123"; 

   string[] sArray = ( new string[]{"Ji","jB"}, ); 

   foreach(string e in sArray) 

  { (e); } 

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

2. Use of Substring

- Substring(Int32, Int32)

Retrieve substrings from this instance. The substring starts at the specified character position and has the specified length.

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

For example, copy the code as follows:

string str = "GTAZB_JiangjBen_123"; 

   int start=3,length=8; 

   ((start-1, length)); 

   Output getsAZB_Jian。 

- Substring(Int32)

Retrieve substrings from this instance. The substring starts at the specified character position.

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

The code copy is as follows:

 string str = "GTAZB_JiangjBen_123"; 

   string tSt; int i=5; 

   tSt = ( - i); 

   (tSt); 

   Outputn_123; 

3. Use of Replace

- Replace(String, String)

Returns a new string where all specified strings appearing in the current instance are replaced with another specified string.

Replace a specific string in a string

The code copy is as follows:

  string str = "GTAZB_JiangjBen_123"; 

   string tSt; 

   tSt = ("123","321"); 

   (tSt); 

   OutputGTAZB_JiangjBen_321 

Delete a specific string Jiangj in a string

string str = "GTAZB_JiangjBen_123"; 

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

   (tSt); 

   OutputGTAZB_Ben_123 

Replace(Char, Char)

Returns a new string where all specified Unicode characters appearing in this instance are replaced with another specified Unicode character.

The following example replaces the blanks between a series of numbers with commas, creating a comma-separated list of values.

public static void Main() 

{ 

   string str = "1 2 3 4 5 6 7 8 9"; 

   ("Original string: \"{0}\"", str); 

   ("CSV string: \"{0}\"", (' ', ',')); 

}

Output:Original string: "1 2 3 4 5 6 7 8 9" 

      CSV string: "1,2,3,4,5,6,7,8,9" 

4. Use of remove

- Remove(Int32, Int32)

Delete a specified number of characters from the specified position in this instance.

Delete a string of the specified length (length) at the specified position (ith)

The code copy is as follows:

string str = "GTAZB_JiangjBen_123"; 

   int i=5,length=8; 

   str=(i, length); 

   (str); 

   OutputGTAZBen_123。 

- Remove(Int32)

Deletes all characters in this string from the specified position to the last position.

 string s = "abc---def"; 

   ("{0}", (3)); 

   Output:abc

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.