C # Several ways to split a string using split

Source: Internet
Author: User

The original Published time: 2009-03-07--from my Baidu article [imported by moving tools]

Recently found a lot of people asked in C # using Split string method, and today there is time so the use of split and other methods of splitting the string to do a summary, I hope to be helpful:
The first method: Open vs.net to create a new console project. 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 result:

Ab
Deab
Deab
De


We see the result is a segmentation with a specified character. What if we want to use multiple characters for segmentation like C,d,e? OK, let's use a different 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 method is to use regular expressions. Create a new console project. Then the using System.Text.RegularExpressions is added 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 result:

Agc
Mac
Ggg
Ytx
What are the benefits 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 * * * * * * * * * * * * * * * * * * * * * *;
If I want to be shown as: I am a teacher, how to do it? We can use the following code:


String Str1= "I * * * * * * * * * * * * * * * * * * * * * *
String[] str2;
Str1=str1. Replace ("* * * *", "*");
Str2=str1. Split (' * ');
foreach (String i in str2)
Console.WriteLine (i.ToString ());


This also gives the right results. But for example,
String Str1= "I * * * * * * * * * * * * * * * * * * * * * *;
I want to show the result: I am a teacher.
If I use the fourth method above, it will produce the following error: I'm a teacher.
There is a space output in the middle, so the output is not the result I want, how to solve it? This goes back to the regular expression (where you can see how powerful it is), you can use the following fifth method:

String Str1= "I * * * * * * * * * * * * * * * * * * * * * *;
string[] str2 = System.Text.RegularExpressions.Regex.Split (str1,@ "[*]+");
foreach (String i in str2)
Console.WriteLine (i.ToString ());

Through the "[*]+"), we have accomplished our goal skillfully.

C # Several ways to split a string using split

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.