asp. net string segmentation function uses method sharing

  • 2020-12-05 17:09:37
  • OfStack

Let's start with a simple example

But the array is 25, not 3. The following method replaces "[ofstack.com]" with a special character, such as $, and executes Split based on that character

For example, the following is divided according to [ofstack.com]


string[] arrstr2 = str.Replace("[ofstack.com]", "$").Split('$');

Now look at the other method, the simplest and most commonly used method, opening vs.net with a partition of the specified character to create a new console project. 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 reference


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

There is another less common method


string str = "reterry[ofstack.com] It's the home of scripts [ofstack.com] The master of "; 
string[] arrstr = str.Split(new char[] { '[', 's', 'o', 's', 'u', 'o', '8', '.', 'c', 'o', 'm', ']' }); 
for (int i = 0; i < arrstr.Length; i++) 
{ 
Response.Write(arrstr[i]); 
}


Related articles: