Summary of asp.net methods for splitting strings

  • 2020-05-16 06:49:29
  • OfStack

The following 11 is introduced:

1. The simplest and most common way to create a new console project is to open vs.net with the segmentation of 1 specified character. Then enter the following program under the Main() method.
 
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

2. Use multiple characters for segmentation
 
string s="abcdeabcdeabcde" 
string[] sArray1=s.Split(new char[3]{'c','d','e'}); 
foreach(string i in sArray1) 
Console.WriteLine(i.ToString()); 

Output the following results:
ab
ab
ab
3. Use regular expressions

Add a reference to using 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

Related articles: