指定适用的 Split
方法重载包含还是省略返回值中的空子字符串。
此枚举具有FlagsAttribute
属性,该属性允许其成员值按位组合。
[System.Flags]
public enum StringSplitOptions
继承 Object
→ValueType
→Enum
→StringSplitOptions
特性 FlagsAttribute
Fields
名称 | 值 | 说明 |
---|---|---|
None | 0 | 返回值包括含有空字符串的数组元素。 |
RemoveEmptyEntries | 1 | 返回值不包括含有空字符串的数组元素。 |
示例
下面的示例使用 StringSplitOptions
枚举来包括或排除 String.Split
方法生成的子字符串:
// This example demonstrates the String() methods that use
// the StringSplitOptions enumeration.
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;
// ------------------------------------------------------------------------------
// Split a string delimited by characters.
// ------------------------------------------------------------------------------
Console.WriteLine("1) Split a string delimited by characters:\n");
// Display the original string and delimiter characters.
Console.WriteLine("1a )The original string is \"{0}\".", s1);
Console.WriteLine("The delimiter character is '{0}'.\n",
charSeparators[0]);
// Split a string delimited by characters and return all elements.
Console.WriteLine("1b) Split a string delimited by characters and " +
"return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);
// Split a string delimited by characters and return all non-empty elements.
Console.WriteLine("1c) Split a string delimited by characters and " +
"return all non-empty elements:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Split the original string into the string and empty string before the
// delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("1d) Split a string delimited by characters and " +
"return 2 elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);
// Split the original string into the string after the delimiter and the
// remainder of the original string after the delimiter.
Console.WriteLine("1e) Split a string delimited by characters and " +
"return 2 non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// ------------------------------------------------------------------------------
// Split a string delimited by another string.
// ------------------------------------------------------------------------------
Console.WriteLine("2) Split a string delimited by another string:\n");
// Display the original string and delimiter string.
Console.WriteLine("2a) The original string is \"{0}\".", s2);
Console.WriteLine("The delimiter string is \"{0}\".\n", stringSeparators[0]);
// Split a string delimited by another string and return all elements.
Console.WriteLine("2b) Split a string delimited by another string and " +
"return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result);
// Split the original string at the delimiter and return all non-empty elements.
Console.WriteLine("2c) Split a string delimited by another string and " +
"return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Split the original string into the empty string before the
// delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("2d) Split a string delimited by another string and " +
"return 2 elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result);
// Split the original string into the string after the delimiter and the
// remainder of the original string after the delimiter.
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);
// Display the array of separated strings using a local function
void Show(string[] entries)
{
Console.WriteLine("The return value contains these {0} elements:", entries.Length);
foreach (string entry in entries)
{
Console.Write("<{0}>", entry);
}
Console.Write("\n\n");
}
/*
This example produces the following results:
1) 分割由字符分隔的字符串::
1a )原始字符串为",ONE,,TWO,,,THREE,,". 分隔符为 ','.
1b) 分割由字符分隔的字符串,并返回所有元素:
返回值包含以下9个元素:
<><ONE><><TWO><><><THREE><><>
1c) Split a string delimited by characters and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>
1d) Split a string delimited by characters and return 2 elements:
The return value contains these 2 elements:
<><ONE,,TWO,,,THREE,,>
1e) Split a string delimited by characters and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO,,,THREE,,>
2) Split a string delimited by another string:
2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
The delimiter string is "[stop]".
2b) Split a string delimited by another string and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>
2c) Split a string delimited by another string and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>
2d) Split a string delimited by another string and return 2 elements:
The return value contains these 2 elements:
<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
2e) Split a string delimited by another string and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
*/
备注
String.Split
方法返回给定字符串中由指定字符或字符串分隔的子字符串的数组。 相邻分隔符产生包含空字符串(""
)的数组元素。 StringSplitOptions
枚举的值指定在返回的数组中是否包含包含空字符串的数组元素。
指定 None
值以调用 String.Split
方法的默认行为,这将返回空子字符串和非空子字符串的数组。 指定 RemoveEmptyEntries
值以使 String.Split
方法返回仅包含非空子字符串的数组。
Split
参数StringSplitOptions.RemoveEmptyEntries
的使用
string test = "程$晓$";
使用:
string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.RemoveEmptyEntries);
输出结果:数组长度为2 temp[0]="程" temp[1]="晓"
;
使用:
string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.None);
或
string[] temp = test.Split('$');
输出结果:数组长度为3 temp[0]="程" temp[1]="晓" temp[2]=""
;