Several Methods for split string in C #

Source: Internet
Author: User
Not written for more than a month Code It seems a little unfamiliar. I have forgotten a lot of basic things. This is not how to separate the lines in a text box in the morning. The enter key is displayed as/T/N. Therefore, the/T/N 1 method string [] sarray = S. split ('C'); only one character can be separated. The method can contain multiple characters, such as C, D, and estring 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

 

3. Regular Expression of the Method

String content = "agcsmallmacsmallgggsmallytx ";

String [] resultstring = RegEx. Split (content, "small", regexoptions. ignorecase)
 
Foreach (string I in resultstring)
 
Console. writeline (I. tostring ());

Recently, someone asked me how to use split. To split the string. Split I hope it will be helpful to you. The following describes several methods:

Method 1: OpenVs.netCreate a console project. ThenMain ()Enter the followingProgram.

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 can see that the result is separated by a specified character. If we want to use multiple characters for segmentation, as shown in figureC, D, EWhat should we do? Well, we use another constructor.:

Change
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 the above two methods,The third method is to use a regular expression. Create a console project. Then addUsing system. Text. regularexpressions;

Main ():To

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
 
UseWhat are the advantages of 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 will returnRegular Expression (here we can see its power)In this case, the fifth method can be used:

String str1 = "I'm *** A ********************* Teacher ";

String [] str2 = system. Text. regularexpressions. RegEx. Split (str1, @ "[*] + ");

Foreach (string I in str2)

Console. writeline (I. tostring ());

Here, we use "[*] +" to cleverly accomplish our goal.

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.