Several ways to split strings in C #

Source: Internet
Author: User
Tags foreach regular expression split
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 am * * * * * * * * * * * * * * * * * * * * *;

If I want to show as: I am a teacher, how to do it? We can use the following code:





String str1= "I am * * * * * * * * * * * * * * * * TEACHERS;

String[] str2;

Str1=str1. Replace ("* *", "*");

Str2=str1. Split (' * ');

foreach (String i in str2)

Console.WriteLine (i.ToString ());





This can also get the correct results. But like

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 * * * * * * * * * *;

string[] str2 = System.Text.RegularExpressions.Regex.Split (str1,@ "[*]+");

foreach (String i in str2)

Console.WriteLine (i.ToString ());



Related Article

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.