C # string Spilt

Source: Internet
Author: User

The first method:

string s = "ABCDEABCDEABCDE";

string[] Sarray = s.split (' C ');

foreach (String i in Sarray)

Console.WriteLine (i.ToString ());

Console.readkey ();

Output the following result: AB Deab Deab de

The second method: we see that the result is a segmentation with a specified character. Use a different construction method to split multiple characters:strings="ABCDEABCDEABCDE";string[] Sarray1=s.split (New Char[3]{'C','D','e'}) ;foreach(stringIinchsArray1) Console.WriteLine (i.ToString ()); You can output the following result: Ababab 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 first:usingSystem.Text.RegularExpressions;stringContent ="Agcyongfa365macyongfa365gggyongfa365ytx";string[] resultstring = regex.split (content,"yongfa365", regexoptions.ignorecase);foreach(stringIinchresultstring) Console.WriteLine (i.ToString ()); Console.readkey (); Output the following result: Agcmacgggytx The Fourth method:stringSTR1 =" * * * * * * * * * * * * * * * * * * * teacher";string[] str2;str1= str1. Replace ("*****","*"); str2= str1. Split ('*');foreach(stringIinchstr2) Console.Write (i.ToString ()); Console.readkey (); most commonly used is: Str. Replace ("/r/n","/ R"). Split ('/ R') The fifth method:stringstr1=" * * * * * * * * * * * * * * * * * * * teacher"I want to show the result: I am a teacher. If I use the fourth method above, it will produce the following error: I am a teacher. 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:stringSTR1 =" * * * * * * * * * * * * * * * * * * * teacher";string[] str2 = System.Text.RegularExpressions.Regex.Split (str1,@"/*+");foreach(stringIinchstr2) Console.Write (i.ToString ()); Console.readkey (); here through/*+ Ingenious completion of our goal. Recommendation: Using regular expressions can save a lot of things, so it is recommended that you use this form: string[] str2 = System.Text.RegularExpressions.Regex.Split (str1, @ "/*+", regexoption 

C # string Spilt

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.