Several methods of using C#.net Split

Source: Internet
Author: User

The first method:

1 string " ABCDEABCDEABCDE " ; 2 string [] Sarray = S.split ('C'); 3 foreach (string in sarray) 4 Console.WriteLine (i.ToString ()); 5 Console.readkey ();

Output the following result:
Ab
Deab
Deab
De

The second method:
We see the result is a segmentation with a specified character. Use a different construction method to split multiple characters:

1 strings="ABCDEABCDEABCDE";2 string[] Sarray1=s.split (New Char[3]{'C','D','e'}) ;3 foreach(stringIinchsArray1)4Console.WriteLine (i.ToString ());

You can output the following results:
Ab
Ab
Ab

The third method:
In addition to these two methods, the third method is to use regular expressions. Create a new console project. Then add the reference: UsingSystem.Text.RegularExpressions;

 1  string  content =  "ABC  shuangfengdefshuangfeng   Ghishuangfeng  JKL " ; 
2 string [] resultstring = Regex.Split (content, shuangfeng " , Regexoptions.ignorecase);
3 foreach (string i in resultstring)
4 Console.WriteLine (i.ToString ());
5 Console.readkey ();

Output the following result:
Abc
Def
Ghi
Jkl

The fourth method:

1 stringSTR1 ="I love * * * * * * * * *";2 string[] str2;3STR1 = str1. Replace ("*****","*");4STR2 = str1. Split ('*');5 foreach(stringIinchstr2)6 Console.Write (i.ToString ());7Console.readkey ();

The most common use is: str. Replace ("\ r \ n", "\ R"). Split (' \ R ')

The Fifth method:
Stringstr1= "I love * * * * * * * * * * * * * * *;
I want to show the result: I love my family.
If I use the fourth method above, it will produce the following error: I love my family. There is a space in the middle of the output, so the output is not the result of hope, this is back to the regular expression, then you can use the following fifth method:

1 string " I love * * * * * * * * * " ; 2 string @" \*+ " ); 3 foreach (string in str2) 4 Console.Write (i.ToString ()); 5 Console.readkey ();

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.