Split split strings in C # and frequently asked questions

Source: Internet
Author: User
Tags foreach regular expression split

Example 1

The code is as follows Copy Code
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

                          &NBSP
 
    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    

  code is as follows copy code
 string s=" ABCDEABCDEABCDE
     string[] Sarray1=s . Split (New char[3]{' C ', ' d ', ' e '});
        foreach (String i in SArray1)
         Console.WriteLine (i.ToString ());
 
    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

The code is as follows Copy Code


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:

  code is as follows copy code
 
     string str1= "I am * * * * * * * * * * * * * * * * * TEACHERS;
       string[] str2;
       str1=str1. Replace ("* *", "*");
       str2=str1. Split (' * ');
      foreach (String i in str2)
       Console.WriteLine (i.ToString ());
 
 
This will also get the correct result. But for example,
    string str1= "I am * * * * * * * * * * * * * * * * * * * * * * * * *

      The result I want to display is: I'm a teacher.
      If I use the fourth method above, I will create the following error: I    is a teacher
       In the middle of a space output, 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:

The code is as follows Copy Code

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


Here through the "[*]+" cleverly completed our goal.

Problems encountered during the use of split


#中Split分隔字符串时, if the delimiter is a character, the length of the character array returned is expected in general.
But when the delimiter is more than one character, such as Str. Split ("| | |"). ToCharArray ()), the returned character array may be more than expected length, the array will appear some values are empty string elements.
At this point we can use regular expressions to split, you may need to be familiar with regular expressions, but the general need to use the regular expressions are relatively simple:

The code is as follows Copy Code
string[] arr = regex.split (str, @ "| | |", regexoptions.ignorecase);

Note: | In a regular expression, a reserved character, which needs to be escaped with "".
Example of breaking a carriage return in a split text field:

  code is as follows copy code
string[] arr = Regex.Split (str, RN)

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.