Split split string
Method 1: Open vs.net to create a console project. Enter the following program in the Main () method.
String s = "abcdeabcdeabcde ";
String [] sArray = s. Split ('C ');
Foreach (string I in sArray)
Console. WriteLine (I. ToString ());
Output the following result: AB
Deab
Deab
De
We can see that the result is separated by a specified character. What if we want to use multiple characters for segmentation, such as c, d, and e? Well, we use another constructor:
Change to string s = "abcdeabcdeabcde
String [] sArray1 = s. Split (new char [3] {'C', 'D', 'E'}); // It is intercepted by characters.
Foreach (string I in sArray1)
Console. WriteLine (I. ToString ());
The following result can be output: AB
AB
AB
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;
Main (): change
System. Text. RegularExpressions
String content = "agcsmallmacsmallgggsmallytx ";
String [] resultString = Regex. Split (content, "small", RegexOptions. IgnoreCase) // static method
Foreach (string I in resultString)
Console. WriteLine (I. ToString ());
Output the following result: agc
Mac
Ggg
Ytx
What are the advantages of using regular expressions? Don't worry, we will see its uniqueness later.
The following describes 4th methods. For example
String str1 = "My ***** is ************************ Teacher ";
If I want to show that I am a teacher, what should I do? We can use the following code:
String str1 = "My ***** is ********************** teacher;
String [] str2;
Str1 = str1.Replace ("*****","*");
Str2 = str1.Split ('*');
Foreach (string I in str2)
Console. WriteLine (I. ToString ());
In this way, you can get the correct result. However, for example
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 are spaces in the middle of the output, so the output result is not what I want. How can this problem be solved? This returns to the regular expression (here we can see its power), then we 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 ());
Author: http://www.cnblogs.com/ivy/archive/2008/07/22/1248839.html