How to split strings in C #

Source: Internet
Author: User
How to split strings in C #

Method 1: Open vs.net to create a console project. Then input the followingProgram.

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 can see that the result is separated by a specified character.

What if we want to use multiple characters for segmentation, such as C, D, and E?

Well, we use another constructor:

Change to string S = "abcdeabcdeabcde

String [] sarray1 = S. Split (New char [3] {'C', ''d', ''e ''});

Foreach (string I in sarray1)

Console. writeline (I. tostring ());

The following result can be output: AB

AB

AB

In addition to the above two methods,

 

The third method is to use a regular expression. Create a console project.

Then add using system. Text. regularexpressions;
'Http: // www.knowsky.com
Main (): change

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

========================================================== ==================================

Reprinted statement:This article from http://hi.baidu.com/e_book/blog/item/3c0cbe1cb67c898e86d6b63f.html

========================================================== ==================================

 

Note for splitting strings using the string. split method in C:

 

 

Note for splitting strings using the string. split method in C:

String. Split provides us with a very flexible way to use it. However, improper use may cause errors. Most people recently used it in code review:

String S = "A | B |: | C: D ";

String [] Ss = S. Split ("|: |". tochararray ());

// Ss [0]:

// Ss [1]: B

// Ss [2]:

// Ss [3]:

// Ss [4]: c

// Ss [5]: d

In fact, his intention is to use "|: |" to separate the string, the goal is to get the array: "A | B" and "C: D ", however, the overload of Split (string) is not directly found, and tochararray () is used. Obviously, the result is incorrect.

Correct usage:

String [] SS1 = S. Split (new [] {"|: |"}, stringsplitoptions. None );

// SS1 [0]: A | B

// SS1 [1]: C: d

The next parameter indicates whether to automatically remove empty data. for example, "A | B |: | C: d |: |" When splitting, three arrays are returned. The last element of the array is null "", if you want to filter out these empty elements, you can use the parameter stringsplitoptions. removeemptyentries: String [] SS1 = S. split (new [] {"|: |"}, stringsplitoptions. removeemptyentries );

Example:

S = "A | B |: | C: d |: | ";

String [] SS1 = S. Split (new [] {"|: |"}, stringsplitoptions. removeemptyentries );

// SS1 [0]: "A | B"

// SS1 [1]: "C: D"

SS1 = S. Split (new [] {"|: |"}, stringsplitoptions. None );

// SS1 [0]: "A | B"

// SS1 [1]: "C: D"

// SS1 [2]: ""

========================================================== ==================================

Reprinted statement:This article from http://luchuxiang.blog.163.com/blog/static/1901506020093610134396/

========================================================== ==================================

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.