C# string split usage and examples, with multiple delimiters and characters

Lionsure 2020-08-30 Original by the website

The Split() method is used to split a string in C#, and the split string is returned as an array.

 

I. Grammar of C# string split

String.Split(params Char[] separator)

 

Overload method:

1. String.Split(Char[] separator, int count)

2. String.Split(Char[] separator, StringSplitOptions options)

3. String.Split(string[] separator, StringSplitOptions options)

4. String.Split(Char[] separator, int count, StringSplitOptions options)

5. String.Split(string[] separator, int count, StringSplitOptions options)

 

 

II. Argument description of C# string split

1. Separator: The separators array, that is, the Unicode character array of the substring in the separates string, does not contain the empty array of separators or null;

2. count: return the maximum number of substrings;

 

3. options: Whether to return an empty array element, it can take 2 values, respectively:

StringSplitOptions.RemoveEmptyEntries does not return array elements containing empty strings;

StringSplitOptions.None returns an array element containing an empty string.

 

 

III. Pay attention to the problem

1. The returned sub-characters(array elements) do not contain a separator;

2. If there is no separator in String, return String as an element of the array;

3. If separator is empty(null or ""), a blank character is used as the separator;

4. The type enclosed in single quotes is "char" type, such as '|'; the type enclosed in double quotes is "string" type, such as "|".

 

 

IV. The namespace and assembly of C# string split

namespace: System;

Assembly: mscorlib.dll.

 

 

V. The examples of C# string split

1. C# split string into array

If you do not pass clear text for security, the login name and password of user are encrypted with Javascript on the client, and the name and password of user are separated by |; the name and password of user are separated after decryption by C# on the server, and the Split() method is used at this time,code show as below:

string str = "username|password";

string[] arr = str.Split('|');//Return array: {"username","password"}
       string[] arr1 = str.Split('.');//Return array: {"username|password"}

string userName = arr[0];
       string pwd = arr[1];

 

2. C# split string on a character or multiple characters

string str = "1,2.2,3,4.4,,5";

string[] arr = str.Split(new Char[] { });//Return array: {"1,2.2,3,4.4,,5"}
       string[] arr1 = str.Split(null);//Return array: {"1,2.2,3,4.4,,5"}

 

string[] arr2 = str.Split(new Char[] { ','});
       //Return array: {"1","2.2","3","4.4","","5"}

string[] arr3 = str.Split(new Char[] { ',', '.' });
       //Return array: {"1","2","2","3","4","4","","5"}

 

3. C# split string on a character or multiple characters and specify the number of returned array elements

string[] arr = str.Split(new Char[] { ',' }, 3);//Return array: {"1","2.2","3,4.4,,5"}

string[] arr1 = str.Split(new Char[] { ',', '.' }, 7);
//Return array: {"1","2","2","3","4","4",",5"}

 

4. C# split string on a character or multiple characters and specify the number of array elements to return and whether to return empty array elements

string[] arr = str.Split(new Char[] { ',' }, 8, StringSplitOptions.None);
//Return array: {"1","2.2","3","4.4","","5"}

string[] arr1 = str.Split(new Char[] { ',' }, 8, StringSplitOptions.RemoveEmptyEntries);
//Return array: {"1","2.2","3","4.4","5"}

 

string[] arr2 = str.Split(new Char[] { ',', '.' }, 8, StringSplitOptions.None);
//Return array: {"1","2",2","3","4","4","","5"}

string[] arr3 = str.Split(new Char[] { ',', '.' }, 8, StringSplitOptions.RemoveEmptyEntries);
//Return array: {"1","2",2","3","4","4","5"}

 

5. C# split string on multiple characters

string[] arr = str.Split(new string[] { ",", "." }, 8, StringSplitOptions.None);
//Return array: {"1","2",2","3","4","4","","5"}

string[] arr1 = str.Split(new string[] { ",", "."; }, 8, StringSplitOptions.RemoveEmptyEntries);
//Return array: {"1","2",2","3","4","4","5"}