C # string divided in several ways Summary

1, common character string is divided

string str = "a,b,c";
string[] arr = str.Split(',');

foreach (string s in arr)
{
    Console.WriteLine(s);
}

-> output:

a
b
c

 

2, using the divided character string character string

string str = "a Font Font b c d Font Font E";
String strTemp = "Font";
String [] = ARR Regex.Split (STR, strTemp, RegexOptions.IgnoreCase);

foreach (string s in arr)
{
    Console.WriteLine(s);
}

-> output:

a
b
c
d
e

 

3, a plurality of character strings divided manner

string str = "a,b,c@d$e";
char[] charTemp = {',', '@', '$'};
string[] arr = str.Split(charTemp);

foreach (string s in arr)
{
    Console.WriteLine(s);
}

or

string str = "a,b,c@d$e";
string[] arr = str.Split(new char[] { ',', '@', '$' });

foreach (string s in arr)
{
    Console.WriteLine(s);
}

-> output:

a
b
c
d
e

 

4, and removing the null character string is divided manner array

string str = "a,,,b,c,d,e";
string[] arr = str.Split(',');

foreach (string s in arr)
{
    Console.WriteLine(s);
}

-> output:

a

 


b
c
d
e

string str = "a,,,b,c,d,e";
string[] arr = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in arr)
{
    Console.WriteLine(s);
}

-> output:

a
b
c
d
e

 

5, the divided region as a string without dividing the character string capitalization of

string str = "bacAdae";
string[] arr = Regex.Split(str, "a", RegexOptions.IgnoreCase);
foreach (string s in arr)
{
    Console.WriteLine(s);
}

-> output:

b
c
d
e

 

Guess you like

Origin www.cnblogs.com/zerosymbol/p/11516136.html