Several methods for splitting strings in c sharp
- 2020-05-05 11:49:29
- OfStack
Single character segmentation
string s="abcdeabcdeabcde";
string[] sArray=s.Split('c');
foreach(string i in sArray)
Console.WriteLine(i.ToString());
Output the following:
ab
deab
deab
de
Multiple character segmentation
string s="abcdeabcdeabcde
string[] sArray1=s.Split(new char[3]{'c','d','e'});
foreach(string i in sArray1)
Console.WriteLine(i.ToString());
The following results can be output:
ab
ab
ab
Multiple character segmentation (regular expression)
string content="agcsmallmacsmallgggsmallytx";
string[]resultString=Regex.Split(content,"small",RegexOptions.IgnoreCase)
foreach(string i in resultString)
Console.WriteLine(i.ToString());
Output the following result :agc
mac
ggg
ytx
string s="abcdeabcdeabcde";
string[] sArray=s.Split('c');
foreach(string i in sArray)
Console.WriteLine(i.ToString());
Output the following:
ab
deab
deab
de
Multiple character segmentation
string s="abcdeabcdeabcde
string[] sArray1=s.Split(new char[3]{'c','d','e'});
foreach(string i in sArray1)
Console.WriteLine(i.ToString());
The following results can be output:
ab
ab
ab
Multiple character segmentation (regular expression)
string content="agcsmallmacsmallgggsmallytx";
string[]resultString=Regex.Split(content,"small",RegexOptions.IgnoreCase)
foreach(string i in resultString)
Console.WriteLine(i.ToString());
Output the following result :agc
mac
ggg
ytx