The string has recently seen many posts in the Forum asked how to use split to split the string, I do some simple summary of split, hope to be helpful to everyone. Here are a few ways to:
The first method: Open vs.net to create a new console item. Then enter the following program under the main () method.
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
We see that the result is a split with a specified character. If we want to use more than one character for segmentation like C,d,e how? OK, let's use another construction method:
Change to 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
In addition to these two methods, the third approach is to use regular expressions. Create a new console item. Then add the using System.Text.RegularExpressions first;
Main (): Change to
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
What's the benefit of using regular expressions? Don't worry, we'll see its uniqueness in the back.
The 4th method is described below. Like what
String Str1= "I * * * * * * * * * * is a * * * * * * * * * *;
The result I want to display is: I am a teacher.
If I use the fourth method above, I will create the following error: I am a teacher
There is a space output in the middle, so the output is not the result I hope, how to solve it? This goes back to the regular expression (see how powerful it is), and you can use the fifth method below:
String Str1= "I * * * * * * * * * * is a * * * * * * * * * *;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.