Several methods for using split split string in C # summary _c# tutorial

Source: Internet
Author: User

The first method:

Copy Code code as follows:

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

The second method:

We see that the result is a split with a specified character. To split multiple characters by using another construction method:

Copy Code code as follows:

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

The third method:

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;

Copy Code code as follows:

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

The fourth method:

Copy Code code as follows:

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

The Fifth method:

Copy Code code as follows:

String str1= * * * * * * * * * * * * * * * * * * * * teacher;
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 in the middle output, so the output is not the desired result, this is back to the regular expression, then you can use the following fifth method:
String str1= * * * * * * * * * * * * * * * * * * * * teacher;
string[] str2 = System.Text.RegularExpressions.Regex.Split (str1,@[*]+);
foreach (String i in str2)
Console.WriteLine (i.ToString ());

Here through [*]+ cleverly accomplished our goal.

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.