この記事は機械翻訳されています。英語版の記事を表示するには、[英語] のチェック ボックスをオンにしてください。また、テキストにマウス ポインターを合わせると、ポップアップ ウィンドウに英語のテキストを表示することもできます。
翻訳
英語

String.Split メソッド (Char[], StringSplitOptions)

 

公開日: 2016年7月

配列内の文字に基づいて文字列を部分文字列に分割します。 部分文字列が空の配列の要素を含めるかどうかを指定することができます。

名前空間:   System
アセンブリ:  mscorlib (mscorlib.dll 内)

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

パラメーター

separator
Type: System.Char[]

この文字列から部分文字列を取り出すために区切り文字として使用する文字配列、区切り文字が含まれていない空の配列、または null

options
Type: System.StringSplitOptions

返される配列から空の配列要素を省略する場合は StringSplitOptions.RemoveEmptyEntries。返される配列に空の配列要素も含める場合は StringSplitOptions.None

戻り値

Type: System.String[]

この文字列を、separator 配列のいずれかまたは複数の要素 (文字) で区切ることによって取り出された部分文字列を格納する配列。 詳細については、「解説」を参照してください。

Exception Condition
ArgumentException

optionsStringSplitOptions 値のいずれでもありません。

Delimiter characters (the characters in the separator array) are not included in the elements of the returned array. For example, if the separator array includes the character "-" and the value of the current string instance is "aa-bb-cc", the method returns an array that contains three elements: "aa", "bb", and "cc".

If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance.

If the options parameter is F:System.StringSplitOptions.RemoveEmptyEntries and the length of this instance is zero, the method returns an empty array.

Each element of separator defines a separate delimiter that consists of a single character. If the options argument is F:System.StringSplitOptions.None, and two delimiters are adjacent or a delimiter is found at the beginning or end of this instance, the corresponding array element contains F:System.String.Empty. For example, if separator includes two elements, "-" and "_", the value of the string instance is "-_aa-_", and the value of the options argument is F:System.StringSplitOptions.None, the method returns a string array with the following five elements:

  1. String.Empty, which represents the empty string that precedes the "-" character at index 0.

  2. String.Empty, which represents the empty string between the "-" character at index 0 and the "_" character at index 1.

  3. "aa",

  4. String.Empty, which represents the empty string that follows the "_" character at index 4.

  5. String.Empty, which represents the empty string that follows the "-" character at index 5.

If the separator parameter is null or contains no characters, white-space characters are assumed to be the delimiters. White-space characters are defined by the Unicode standard and return true if they are passed to the M:System.Char.IsWhiteSpace(System.Char) method.

If the separator parameter in the call to this method overload is null, compiler overload resolution fails. To unambiguously identify the called method, your code must indicate the type of the null. The following example shows several ways to unambiguously identify this overload.

string phrase = "The quick  brown fox";
string[] words;

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

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

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

The M:System.String.Split(System.Char[],System.StringSplitOptions) method extracts the substrings in this string that are delimited by one or more of the characters in the separator parameter, and returns those substrings as elements of an array.

The Overload:System.String.Split method looks for delimiters by performing comparisons using case-sensitive ordinal sort rules. For more information about word, string, and ordinal sorts, see the T:System.Globalization.CompareOptions enumeration.

TheOverload:System.String.Split methods allocate memory for the returned array object and a T:System.String object for each array element. If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the Overload:System.String.IndexOf or Overload:System.String.IndexOfAny method, and optionally the Overload:System.String.Compare method, to locate a substring within a string.

If you are splitting a string at a separator character, use the Overload:System.String.IndexOf or Overload:System.String.IndexOfAny method to locate a separator character in the string. If you are splitting a string at a separator string, use the Overload:System.String.IndexOf or Overload:System.String.IndexOfAny method to locate the first character of the separator string. Then use the Overload:System.String.Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.

In addition, if the same set of characters is used to split strings in multiple Overload:System.String.Split method calls, consider creating a single array and referencing it in each method call. This significantly reduces the additional overhead of each method call.

呼び出し時の注意:

In the net_v35_short and earlier versions, if the M:System.String.Split(System.Char[]) method is passed a separator that is null or contains no characters, the method uses a slightly different set of characters to split the string than the M:System.String.Trim(System.Char[]) method does to trim the string. In the net_v40_short, both methods use an identical set of Unicode white-space characters.

The following example uses the T:System.StringSplitOptions enumeration to include or exclude substrings generated by the Overload:System.String.Split method.

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

*/

ユニバーサル Windows プラットフォーム
8 以降で使用可能
.NET Framework
2.0 以降で使用可能
ポータブル クラス ライブラリ
サポート対象: 移植可能 .NET プラットフォーム
Silverlight
2.0 以降で使用可能
Windows Phone Silverlight
7.0 以降で使用可能
Windows Phone
8.1 以降で使用可能
トップに戻る
表示: