Method 1:
String S = abcdeabcdeabcde;
String [] sarray = S. Split (C );
Foreach (string I in sarray)
Console. writeline (I. tostring ());
Output the following results:
AB
Deab
Deab
De
Method 2:
We can see that the result is separated by a specified character. Use another constructor to separate multiple characters:
String S = abcdeabcdeabcde
String [] sarray1 = S. Split (New char [3] {c, d, e });
Foreach (string I in sarray1)
Console. writeline (I. tostring ());
You can output the following results:
AB
AB
AB
Method 3:
In addition to the above two methods, the third method is to use a regular expression. Create a console project. Then add using system. Text. regularexpressions;
System. Text. regularexpressions
String content = agcsmallmacsmallgggsmallytx;
String [] resultstring = RegEx. Split (content,Small, Regexoptions. ignorecase)
Foreach (string I in resultstring)
Console. writeline (I. tostring ());
Output the following results:
AGC
Mac
Ggg
Ytx
Method 4:
String str1 = I'm *** A *** teacher;
String [] str2;
Str1 = str1.replace (*****,*);
Str2 = str1.split (*);
Foreach (string I in str2)
Console. writeline (I. tostring ());
Method 5:
String str1 = I'm a ********************** teacher;
The result I want to display is: I am a teacher.
If I use the fourth method above, the following error will occur: I am a teacher. There is a space in the middle of the output, so the output result is not the expected result, which returns to the regular expression, then you can use the fifth method below:
String str1 = I'm a ********************** teacher;
String [] str2 = system. Text. regularexpressions. RegEx. Split (str1,@ [*] +);
Foreach (string I in str2)
Console. writeline (I. tostring ());
Here, we use [*] + to skillfully accomplish our goal.