System.String.Split 方法 (Char[], Int32, StringSplitOptions)

方法描述

返回的字符串数组包含此字符串中的子字符串(由指定 Unicode 字符数组的元素分隔)。 参数指定要返回子字符串的最大数量,以及是否返回空数组元素。

语法定义(C# System.String.Split 方法 (Char[], Int32, StringSplitOptions) 的用法)

[ComVisibleAttribute(false)]
public string[] Split(
	char[] separator,
	int count,
	StringSplitOptions options
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
separator System-Char[] 分隔此字符串中子字符串的 Unicode 字符数组、不包含分隔符的空数组或 null。
count System-Int32 要返回的子字符串的最大数量。
options System-StringSplitOptions 要省略返回的数组中的空数组元素,则为 StringSplitOptions.RemoveEmptyEntries;要包含返回的数组中的空数组元素,则为 StringSplitOptions.None。
返回值 System.String[] 一个数组,其元素包含此字符串中的子字符串,这些子字符串由 separator 中的一个或多个字符分隔。 有关更多信息,请参见“备注”一节。

提示和注释

返回的数组元素中不包含分隔符字符。

如果此实例不包含 separator 中的任何字符,或者 count 参数为 1,则返回的数组由包含此实例的单个元素组成。 如果 separator 参数为 null 或不包含任何字符,则采用空白字符作为分隔符。 空白字符由 Unicode 标准定义,如果将它们传递给 Char.IsWhiteSpace 方法,将返回 true。 但是,如果调用中的 separator 参数对于此方法重载为 null,则编译器重载决策失败。 要明确标识调用的方法,您的代码必须指示 null 类型。 下面的示例显示明确标识此重载的集中方法。

C#

VB

复制

string phrase = "The quick brown fox";

string[] words;

words = phrase.Split(default(Char[]), 3, StringSplitOptions.RemoveEmptyEntries);

words = phrase.Split((char[]) null, 3, StringSplitOptions.RemoveEmptyEntries);

words = phrase.Split(null as char[], 3, StringSplitOptions.RemoveEmptyEntries);

如果 count 参数为零,或者 options 参数为 RemoveEmptyEntries 且此实例的长度为零,则返回空数组。

separator 的每一个元素都定义一个单独的分隔符字符。 如果 options 参数为 None,且两个分隔符相邻,或者在此实例的开头或末尾找到分隔符,则相对应的数组元素包含 Empty。

如果此实例中的子字符串比 count 多,则在第一个 count 减去 1 的返回值元素中返回第一个 count 减去 1 的子字符串,此实例中剩余的字符在返回值的最后一个元素中返回。

如果 count 大于子字符串的数量,则返回可用子字符串,并且不会引发异常。

性能注意事项

Split 方法为返回的数组对象分配内存,同时还为每一个数组元素分配一个 String 对象。 如果您的应用程序要求达到最佳性能,或者如果在您的应用程序中内存分配管理很关键,请考虑使用 IndexOf 或 IndexOfAny 方法,也可以选择使用 Compare 方法,在字符串中定位子字符串。

如果在分隔符字符处分割字符串,请使用 IndexOf 或 IndexOfAny 方法在字符串中定位分隔符字符。 如果在分隔符字符串处分割字符串,请使用 IndexOf 或 IndexOfAny 方法定位分隔符字符串的第一个字符。 然后使用 Compare 方法确定第一个字符后面的字符是否等于分隔符字符串的其余字符。

此外,如果在多个 Split 方法调用中使用相同的字符集拆分字符串,请考虑创建一个数组并在每个方法调用中都引用该数组。 这可以极大地减少每个方法调用的额外系统开销。

对调用者的说明

在 .NET Framework 3.5 和早期版本中,如果为 Split 方法传递了值为 null 或不包含字符的 separator 参数,该方法用来分割字符串的字符集将与 Trim 方法用来修整字符串的字符集略有不同。 在 .NET Framework 4 中,这两种方法使用完全相同的 Unicode 空白字符集。

System.String.Split 方法 (Char[], Int32, StringSplitOptions)例子

下面的示例使用 StringSplitOptions 枚举来包含或排除由 Split 方法生成的子字符串。

// This example demonstrates the String() methods that use
// the StringSplitOptions enumeration.
using System;

class Sample 
{
    public static void Main() 
    {
    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.
    public static 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) Split a string delimited by characters:

1a )The original string is ",ONE,,TWO,,,THREE,,".
The delimiter character is ','.

1b) Split a string delimited by characters and return all elements:
The return value contains these 9 elements:
<><><><><><>

1c) Split a string delimited by characters and return all non-empty elements:
The return value contains these 3 elements:


1d) Split a string delimited by characters and return 2 elements:
The return value contains these 2 elements:
<>

1e) Split a string delimited by characters and return 2 non-empty elements:
The return value contains these 2 elements:


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:
<><><><><><>

2c) Split a string delimited by another string and return all non-empty elements:
The return value contains these 3 elements:


2d) Split a string delimited by another string and return 2 elements:
The return value contains these 2 elements:
<>

2e) Split a string delimited by another string and return 2 non-empty elements:
The return value contains these 2 elements:


*/

异常

异常 异常描述
ArgumentOutOfRangeException count 为负。
ArgumentException options 不是 StringSplitOptions 值之一。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。