String.Split方法用于按照指定的字符串来拆分原有字符串,并返回拆分后得到的字符串数组。
String.Split的语法包括如下8种,根据分隔符为字符(char)或者字符串(string)可分为两类,这两类的第1、第2种用法均包含在第3、第4种用法里( 见所举示例代码中的的1b、1d ,2b、2d)。
//第一类:字符char作为分隔符
1. Split(Char[]) //根据指定的分隔字符将字符串拆分为子字符串。
2. Split(Char[], Int32) //根据指定的分隔字符将一个字符串拆分成最大数量的子字符串。
3. Split(Char[], StringSplitOptions) //根据指定的分隔字符和选项将字符串拆分为子字符串。
4. Split(Char[], Int32, StringSplitOptions) //基于指定的分隔字符和(可选)选项将字符串拆分为最大数量的子字符串。
/*——————————————————————————————————————————————————————————————————————————*/
//第二类:字符串string作为分隔符
1. Split(String[]) //根据指定的分隔字符串将字符串拆分为子字符串。
2. Split(String[], Int32) //根据指定的分隔字符串将一个字符串拆分成最大数量的子字符串。
3. Split(String[], StringSplitOptions) //基于指定的分隔字符串和(可选)选项将字符串拆分为子字符串。
4. Split(String[], Int32, StringSplitOptions) //基于指定的分隔字符串和(可选)选项将字符串拆分为最大数量的子字符串。
最好的理解方式还是学习代码,现举一个例子,包括上述各种方法的使用。
代码如下。两类的第1、第2种用法包含在第3、第4种用法里( 即下例代码中的的1b、1d ,2b、2d)
class Program
{
static void Main(string[] args)
{
string s1 = ",ONE,,TWO,,,THREE,,";
string s2 = "[stop]" +
"ONE[stop][stop]" +
"TWO[stop][stop][stop]" +
"THREE[stop][stop]";
char[] charSeparators = new char[] { ',' };
string[] stringSeparators = new string[] { "[stop]" };
string[] result;
// ------------------------------------------------------------------------------
// 第一类:字符类型“char”作为分隔符的用法
// ------------------------------------------------------------------------------
Console.WriteLine("1) Split a string delimited by characters:\n");
Console.WriteLine("1a) The original string is \"{0}\".", s1); //1a) The original string is ",ONE,,TWO,,,THREE,,".
Console.WriteLine($"The delimiter character is '{charSeparators[0]}'.\n"); //The delimiter character is ','.
Console.WriteLine("1b) Split a string delimited by characters and " +
"return all elements:"); //1b) Split a string delimited by characters and return all elements:
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result); //<><ONE><><TWO><><><THREE><><>
Console.WriteLine("1c) Split a string delimited by characters and " +
"return all non-empty elements:"); //1c) Split a string delimited by characters and return all non-empty elements:
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result); //<ONE><TWO><THREE>
Console.WriteLine("1d) Split a string delimited by characters and " +
"return 2 elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result); //<><ONE,,TWO,,,THREE,,>
Console.WriteLine("1e) Split a string delimited by characters and " +
"return 2 non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result); //<ONE><TWO,,,THREE,,>
// ------------------------------------------------------------------------------
// 第二类:字符串类型“string作为分隔符的用法
// ------------------------------------------------------------------------------
Console.WriteLine("2) Split a string delimited by another string:\n");
Console.WriteLine($"2a) The original string is \"{s2}\".");
Console.WriteLine("The delimiter string is \"{0}\".\n", stringSeparators[0]); //The delimiter string is "[stop]".
Console.WriteLine("2b) Split a string delimited by another string and " +
"return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result); //<><ONE><><TWO><><><THREE><><>
Console.WriteLine("2c) Split a string delimited by another string and " +
"return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result); //<ONE><TWO><THREE>
Console.WriteLine("2d) Split a string delimited by another string and " +
"return 2 elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result); //<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
Console.WriteLine("2e) Split a string delimited by another string and " +
"return 2 non-empty elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result); //<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
void Show(string[] entries)
{
Console.WriteLine($"The return value contains these {entries.Length} elements:");
foreach (string entry in entries)
{
Console.Write($"<{entry}>");
}
Console.Write("\n\n");
}
}
}