英語で読む

次の方法で共有


String.Split メソッド

定義

指定した文字列または Unicode 文字配列の要素で区切られた、このインスタンス内の部分文字列を含む文字列配列を返します。

オーバーロード

Split(String, Int32, StringSplitOptions)

指定した区切り文字列とオプション (オプション) に基づいて、文字列を部分文字列の最大数に分割します。

Split(Char[], Int32, StringSplitOptions)

指定した区切り文字とオプションに基づいて、文字列を部分文字列の最大数に分割します。

Split(Char, Int32, StringSplitOptions)

指定した区切り文字とオプション (オプション) に基づいて、文字列を部分文字列の最大数に分割します。 指定された文字区切り記号に基づいて文字列を最大数の部分文字列に分割します。必要に応じて、結果から空の部分文字列を省略します。

Split(String[], StringSplitOptions)

指定した区切り文字列とオプションに基づいて、文字列を部分文字列に分割します。

Split(String, StringSplitOptions)

指定された文字列区切り記号に基づいて、文字列を部分文字列に分割します。

Split(Char[])

指定した区切り文字に基づいて、文字列を部分文字列に分割します。

Split(Char[], Int32)

指定した区切り文字に基づいて、文字列を部分文字列の最大数に分割します。

Split(Char, StringSplitOptions)

指定した区切り文字とオプション (オプション) に基づいて、文字列を部分文字列に分割します。

Split(ReadOnlySpan<Char>)

指定した区切り文字に基づいて、文字列を部分文字列に分割します。

Split(String[], Int32, StringSplitOptions)

指定した区切り文字列とオプションに基づいて、文字列を部分文字列の最大数に分割します。

Split(Char[], StringSplitOptions)

指定した区切り文字とオプションに基づいて、文字列を部分文字列に分割します。

注釈

Split は、区切られた文字列を部分文字列に分割するために使用されます。 文字配列または文字列配列を使用して、0 個以上の区切り文字または文字列を指定できます。 区切り文字が指定されていない場合、文字列は空白文字で分割されます。

Split メソッドのオーバーロードを使用すると、メソッド (Split(Char[], Int32) メソッド) によって返される部分文字列の数を制限し、結果に空の文字列を含めるか、結果に部分文字列をトリミングするか (Split(Char[], StringSplitOptions) メソッドと Split(String[], StringSplitOptions) メソッド)、または両方を実行するか (Split(Char[], Int32, StringSplitOptions) メソッドと Split(String[], Int32, StringSplitOptions) メソッド) を指定できます。

ヒント

Split メソッドは、区切られた文字列を部分文字列に分割する最善の方法であるとは限りません。 区切り文字列のすべての部分文字列を抽出しない場合、または区切り文字のセットではなくパターンに基づいて文字列を解析する場合は、正規表現の使用を検討するか、文字のインデックスを返す検索メソッドの 1 つを Substring メソッドと組み合わせることを検討してください。 詳細については、「文字列から部分文字列を抽出する」を参照してください。

次の例は、String.Split()の 3 つの異なるオーバーロードを示しています。 最初の例では、Split(Char[]) オーバーロードを呼び出し、1 つの区切り記号を渡します。

C#
string s = "You win some. You lose some.";

string[] subs = s.Split(' ');

foreach (var sub in subs)
{
    Console.WriteLine($"Substring: {sub}");
}

// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some.
// Substring: You
// Substring: lose
// Substring: some.

ご覧のように、ピリオド文字 (.) は 2 つの部分文字列に含まれています。 ピリオド文字を除外する場合は、ピリオド文字を追加の区切り文字として追加できます。 次の例では、これを行う方法を示します。

C#
string s = "You win some. You lose some.";

string[] subs = s.Split(' ', '.');

foreach (var sub in subs)
{
    Console.WriteLine($"Substring: {sub}");
}

// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some
// Substring:
// Substring: You
// Substring: lose
// Substring: some
// Substring:

ピリオドは部分文字列から削除されますが、2 つの余分な空の部分文字列が含まれるようになりました。 これらの空の部分文字列は、単語とその後のピリオドの間の部分文字列を表します。 結果の配列から空の部分文字列を省略するには、Split(Char[], StringSplitOptions) オーバーロードを呼び出し、options パラメーターの StringSplitOptions.RemoveEmptyEntries を指定します。

C#
string s = "You win some. You lose some.";
char[] separators = new char[] { ' ', '.' };

string[] subs = s.Split(separators, StringSplitOptions.RemoveEmptyEntries);

foreach (var sub in subs)
{
    Console.WriteLine($"Substring: {sub}");
}

// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some
// Substring: You
// Substring: lose
// Substring: some

String.Split() の個々のオーバーロードのセクションには、さらに例が含まれています。

Split(String, Int32, StringSplitOptions)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した区切り文字列とオプション (オプション) に基づいて、文字列を部分文字列の最大数に分割します。

C#
public string[] Split (string? separator, int count, StringSplitOptions options = System.StringSplitOptions.None);
C#
public string[] Split (string separator, int count, StringSplitOptions options = System.StringSplitOptions.None);

パラメーター

separator
String

このインスタンス内の部分文字列を区切る文字列。

count
Int32

配列に必要な要素の最大数。

options
StringSplitOptions

部分文字列をトリミングし、空の部分文字列を含めるかどうかを指定する列挙値のビットごとの組み合わせ。

戻り値

String[]

separatorで区切られた、このインスタンスの最大 count 部分文字列を含む配列。

注釈

文字列が既に count - 1 回分割されているが、文字列の末尾に達していない場合、返される配列の最後の文字列には、このインスタンスの残りの末尾の部分文字列がそのまま含まれます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Split(Char[], Int32, StringSplitOptions)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した区切り文字とオプションに基づいて、文字列を部分文字列の最大数に分割します。

C#
public string[] Split (char[] separator, int count, StringSplitOptions options);
C#
public string[] Split (char[]? separator, int count, StringSplitOptions options);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public string[] Split (char[] separator, int count, StringSplitOptions options);

パラメーター

separator
Char[]

この文字列内の部分文字列を区切る文字の配列、区切り記号を含まない空の配列、または null

count
Int32

返される部分文字列の最大数。

options
StringSplitOptions

部分文字列をトリミングし、空の部分文字列を含めるかどうかを指定する列挙値のビットごとの組み合わせ。

戻り値

String[]

separator内の 1 つ以上の文字で区切られた、この文字列内の部分文字列を含む配列。 詳細については、「解説」セクションを参照してください。

属性

例外

count は負の値です。

options は、StringSplitOptions 値の 1 つではありません。

次の例では、StringSplitOptions 列挙型を使用して、Split メソッドによって生成された部分文字列を含めるか除外します。

C#
// This example demonstrates the String.Split() methods that use
// the StringSplitOptions enumeration.

// Example 1: Split a string delimited by characters
Console.WriteLine("1) Split a string delimited by characters:\n");

string s1 = ",ONE,, TWO,, , THREE,,";
char[] charSeparators = new char[] { ',' };
string[] result;

Console.WriteLine($"The original string is: \"{s1}\".");
Console.WriteLine($"The delimiter character is: '{charSeparators[0]}'.\n");

// Split the string and return all elements
Console.WriteLine("1a) Return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);

// Split the string and return all elements with whitespace trimmed
Console.WriteLine("1b) Return all elements with whitespace trimmed:");
result = s1.Split(charSeparators, StringSplitOptions.TrimEntries);
Show(result);

// Split the string and return all non-empty elements
Console.WriteLine("1c) Return all non-empty elements:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string and return all non-whitespace elements with whitespace trimmed
Console.WriteLine("1d) Return all non-whitespace elements with whitespace trimmed:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Split the string into only two elements, keeping the remainder in the last match
Console.WriteLine("1e) Split into only two elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);

// Split the string into only two elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("1f) Split into only two elements with whitespace trimmed:");
result = s1.Split(charSeparators, 2, StringSplitOptions.TrimEntries);
Show(result);

// Split the string into only two non-empty elements, keeping the remainder in the last match
Console.WriteLine("1g) Split into only two non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string into only two non-whitespace elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("1h) Split into only two non-whitespace elements with whitespace trimmed:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Example 2: Split a string delimited by another string
Console.WriteLine("2) Split a string delimited by another string:\n");

string s2 = "[stop]" +
            "ONE[stop] [stop]" +
            "TWO  [stop][stop]  [stop]" +
            "THREE[stop][stop]  ";
string[] stringSeparators = new string[] { "[stop]" };

Console.WriteLine($"The original string is: \"{s2}\".");
Console.WriteLine($"The delimiter string is: \"{stringSeparators[0]}\".\n");

// Split the string and return all elements
Console.WriteLine("2a) Return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result);

// Split the string and return all elements with whitespace trimmed
Console.WriteLine("2b) Return all elements with whitespace trimmed:");
result = s2.Split(stringSeparators, StringSplitOptions.TrimEntries);
Show(result);

// Split the string and return all non-empty elements
Console.WriteLine("2c) Return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string and return all non-whitespace elements with whitespace trimmed
Console.WriteLine("2d) Return all non-whitespace elements with whitespace trimmed:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Split the string into only two elements, keeping the remainder in the last match
Console.WriteLine("2e) Split into only two elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result);

// Split the string into only two elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("2f) Split into only two elements with whitespace trimmed:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.TrimEntries);
Show(result);

// Split the string into only two non-empty elements, keeping the remainder in the last match
Console.WriteLine("2g) Split into only two non-empty elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string into only two non-whitespace elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("2h) Split into only two non-whitespace elements with whitespace trimmed:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Display the array of separated strings using a local function
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");
}

/*
This example produces the following results:

1) Split a string delimited by characters:

The original string is: ",ONE,, TWO,, , THREE,,".
The delimiter character is: ','.

1a) Return all elements:
The return value contains these 9 elements:
<><ONE><>< TWO><>< >< THREE><><>

1b) Return all elements with whitespace trimmed:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

1c) Return all non-empty elements:
The return value contains these 4 elements:
<ONE>< TWO>< >< THREE>

1d) Return all non-whitespace elements with whitespace trimmed:
The return value contains these 3 elements:
<ONE><TWO><THREE>

1e) Split into only two elements:
The return value contains these 2 elements:
<><ONE,, TWO,, , THREE,,>

1f) Split into only two elements with whitespace trimmed:
The return value contains these 2 elements:
<><ONE,, TWO,, , THREE,,>

1g) Split into only two non-empty elements:
The return value contains these 2 elements:
<ONE>< TWO,, , THREE,,>

1h) Split into only two non-whitespace elements with whitespace trimmed:
The return value contains these 2 elements:
<ONE><TWO,, , THREE,,>

2) Split a string delimited by another string:

The original string is: "[stop]ONE[stop] [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]  ".
The delimiter string is: "[stop]".

2a) Return all elements:
The return value contains these 9 elements:
<><ONE>< ><TWO  ><><  ><THREE><><  >

2b) Return all elements with whitespace trimmed:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

2c) Return all non-empty elements:
The return value contains these 6 elements:
<ONE>< ><TWO  ><  ><THREE><  >

2d) Return all non-whitespace elements with whitespace trimmed:
The return value contains these 3 elements:
<ONE><TWO><THREE>

2e) Split into only two elements:
The return value contains these 2 elements:
<><ONE[stop] [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]  >

2f) Split into only two elements with whitespace trimmed:
The return value contains these 2 elements:
<><ONE[stop] [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]>

2g) Split into only two non-empty elements:
The return value contains these 2 elements:
<ONE>< [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]  >

2h) Split into only two non-whitespace elements with whitespace trimmed:
The return value contains these 2 elements:
<ONE><TWO  [stop][stop]  [stop]THREE[stop][stop]>

*/

注釈

区切り文字は、返される配列の要素には含まれません。

このインスタンスに separator内の文字が含まれていない場合、または count パラメーターが 1 の場合、返される配列は、このインスタンスを含む 1 つの要素で構成されます。

separator パラメーターが null または文字を含まない場合、空白文字は区切り記号と見なされます。 空白文字は Unicode 標準によって定義され、Char.IsWhiteSpace メソッドは渡された場合に true を返します。

char[] separator パラメーターの null を渡すには、null の型を指定して、Split(String[], Int32, StringSplitOptions)などの他のいくつかのオーバーロードからの呼び出しを明確にする必要があります。 次の例は、このオーバーロードを明確に識別するいくつかの方法を示しています。

C#
string phrase = "The quick  brown fox";

_ = phrase.Split(default(char[]), 3, StringSplitOptions.RemoveEmptyEntries);

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

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

count パラメーターが 0 の場合、または options パラメーターが RemoveEmptyEntries で、このインスタンスの長さが 0 の場合は、空の配列が返されます。

separator の各要素は、個別の区切り文字を定義します。 options パラメーターが Noneされ、2 つの区切り記号が隣接している場合、またはこのインスタンスの先頭または末尾に区切り記号が見つかった場合、対応する配列要素には Emptyが含まれます。

このインスタンスに count を超える部分文字列がある場合、最初の count から 1 個の部分文字列を引いた部分文字列が戻り値の最初の count から 1 要素を引いた値で返され、このインスタンスの残りの文字は戻り値の最後の要素で返されます。

count が部分文字列の数より大きい場合は、使用可能な部分文字列が返され、例外はスローされません。

パフォーマンスに関する考慮事項

Split メソッドは、返された配列オブジェクトにメモリを割り当て、各配列要素に String オブジェクトを割り当てます。 アプリケーションで最適なパフォーマンスが必要な場合、またはアプリケーションでメモリ割り当ての管理が重要な場合は、IndexOf メソッドまたは IndexOfAny メソッド、および必要に応じて Compare メソッドを使用して、文字列内の部分文字列を検索することを検討してください。

区切り文字で文字列を分割する場合は、IndexOf メソッドまたは IndexOfAny メソッドを使用して、文字列内の区切り文字を検索します。 区切り文字列で文字列を分割する場合は、IndexOf または IndexOfAny メソッドを使用して、区切り文字列の最初の文字を検索します。 次に、Compare メソッドを使用して、最初の文字の後の文字が区切り文字の残りの文字と等しいかどうかを判断します。

さらに、同じ文字セットを使用して複数の Split メソッド呼び出しで文字列を分割する場合は、1 つの配列を作成し、各メソッド呼び出しでそれを参照することを検討してください。 これにより、各メソッド呼び出しの追加オーバーヘッドが大幅に削減されます。

注意 (呼び出し元)

.NET Framework 3.5 以前のバージョンでは、Split(Char[]) メソッドに null または文字が含まれていない separator が渡された場合、このメソッドは、文字列をトリミングするために Trim(Char[]) メソッドとは少し異なる空白文字のセットを使用して文字列を分割します。 .NET Framework 4 以降では、どちらのメソッドも同じ Unicode 空白文字のセットを使用します。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Split(Char, Int32, StringSplitOptions)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した区切り文字とオプション (オプション) に基づいて、文字列を部分文字列の最大数に分割します。 指定された文字区切り記号に基づいて文字列を最大数の部分文字列に分割します。必要に応じて、結果から空の部分文字列を省略します。

C#
public string[] Split (char separator, int count, StringSplitOptions options = System.StringSplitOptions.None);

パラメーター

separator
Char

このインスタンスの部分文字列を区切る文字。

count
Int32

配列に必要な要素の最大数。

options
StringSplitOptions

部分文字列をトリミングし、空の部分文字列を含めるかどうかを指定する列挙値のビットごとの組み合わせ。

戻り値

String[]

separatorで区切られた、このインスタンスの最大 count 部分文字列を含む配列。

注釈

文字列が既に count - 1 回分割されているが、文字列の末尾に達していない場合、返される配列の最後の文字列には、このインスタンスの残りの末尾の部分文字列がそのまま含まれます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Split(String[], StringSplitOptions)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した区切り文字列とオプションに基づいて、文字列を部分文字列に分割します。

C#
public string[] Split (string[] separator, StringSplitOptions options);
C#
public string[] Split (string[]? separator, StringSplitOptions options);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public string[] Split (string[] separator, StringSplitOptions options);

パラメーター

separator
String[]

この文字列内の部分文字列を区切る文字列の配列、区切り記号を含まない空の配列、または null

options
StringSplitOptions

部分文字列をトリミングし、空の部分文字列を含めるかどうかを指定する列挙値のビットごとの組み合わせ。

戻り値

String[]

separator内の 1 つ以上の文字列で区切られた、この文字列内の部分文字列を要素に含む配列。 詳細については、「解説」セクションを参照してください。

属性

例外

options は、StringSplitOptions 値の 1 つではありません。

次の例は、StringSplitOptions.NoneStringSplitOptions.RemoveEmptyEntriesと等しい options パラメーターを使用して文字列の String.Split(String[], StringSplitOptions) メソッドを呼び出すことによって返される配列の違いを示しています。

C#
string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] { "[stop]" };
string[] result;

// Display the original string and delimiter string.
Console.WriteLine($"Splitting the string:\n   \"{source}\".");
Console.WriteLine();
Console.WriteLine($"Using the delimiter string:\n   \"{stringSeparators[0]}\"");
Console.WriteLine();

// Split a string delimited by another string and return all elements.
result = source.Split(stringSeparators, StringSplitOptions.None);
Console.WriteLine($"Result including all elements ({result.Length} elements):");
Console.Write("   ");
foreach (string s in result)
{
    Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
Console.WriteLine();
Console.WriteLine();

// Split delimited by another string and return all non-empty elements.
result = source.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"Result including non-empty elements ({result.Length} elements):");
Console.Write("   ");
foreach (string s in result)
{
    Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
Console.WriteLine();

// The example displays the following output:
//    Splitting the string:
//       "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
//
//    Using the delimiter string:
//       "[stop]"
//
//    Result including all elements (9 elements):
//       '<>' 'ONE' '<>' 'TWO' '<>' '<>' 'THREE' '<>' '<>'
//
//    Result including non-empty elements (3 elements):
//       'ONE' 'TWO' 'THREE'

次の例では、句読点と空白文字を含む区切り記号の配列を定義します。 この配列を StringSplitOptions.RemoveEmptyEntries の値と共に Split(String[], StringSplitOptions) メソッドに渡すと、文字列の個々の単語で構成される配列が返されます。

C#
string[] separators = { ",", ".", "!", "?", ";", ":", " " };
string value = "The handsome, energetic, young dog was playing with his smaller, more lethargic litter mate.";
string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (var word in words)
    Console.WriteLine(word);

// The example displays the following output:
//       The
//       handsome
//       energetic
//       young
//       dog
//       was
//       playing
//       with
//       his
//       smaller
//       more
//       lethargic
//       litter
//       mate

このメソッドは、options 引数を StringSplitOptions.RemoveEmptyEntriesに設定して呼び出されることに注意してください。 これにより、返された配列に、句読点と空白文字の間の空の部分文字列の一致を表す String.Empty 値が含まれるのを防ぐことができます。

注釈

文字列が既知の文字列セットで区切られている場合は、Split メソッドを使用して部分文字列に区切ることができます。

区切り記号文字列は、返される配列の要素には含まれません。 たとえば、separator 配列に文字列 "--" が含まれており、現在の文字列インスタンスの値が "aa--bb--cc" の場合、メソッドは 3 つの要素 "aa"、"bb"、および "cc" を含む配列を返します。

このインスタンスに separatorの文字列が含まれていない場合、返される配列は、このインスタンスを含む 1 つの要素で構成されます。

options パラメーターが RemoveEmptyEntries で、このインスタンスの長さが 0 の場合、このメソッドは空の配列を返します。

separator の各要素は、1 つ以上の文字で構成される個別の区切り記号を定義します。 options 引数が Noneされ、2 つの区切り記号が隣接している場合、またはこのインスタンスの先頭または末尾に区切り記号が見つかった場合は、対応する配列要素に String.Emptyが含まれます。 たとえば、separator に "-" と "_" の 2 つの要素が含まれており、文字列インスタンスの値が "-_aa-_" で、options 引数の値が None場合、メソッドは次の 5 つの要素を持つ文字列配列を返します。

  1. String.Empty:インデックス 0 の "-" 部分文字列の前にある空の文字列を表します。

  2. String.Empty:インデックス 0 の "-" 部分文字列とインデックス 1 の "_" 部分文字列の間の空の文字列を表します。

  3. "aa"

  4. String.Empty:インデックス 4 の "-" 部分文字列の後に続く空の文字列を表します。

  5. String.Empty:インデックス 5 の "_" 部分文字列の後に続く空の文字列を表します。

区切り記号の配列

separator のいずれかの要素が複数の文字で構成されている場合、部分文字列全体は区切り記号と見なされます。 たとえば、separator の要素の 1 つが "10" の場合、文字列 "This10is10a10string" を分割しようとすると、次の 4 要素配列 {"This"、"is"、"a"、"string." }が返されます。

separator パラメーターが null または空でない文字列を含まない場合、空白文字は区切り記号と見なされます。 空白文字は Unicode 標準によって定義され、Char.IsWhiteSpace メソッドは渡された場合に true を返します。

string[] separator パラメーターの null を渡すには、null の型を指定して、Split(Char[], StringSplitOptions)などの他のいくつかのオーバーロードからの呼び出しを明確にする必要があります。 次の例は、このオーバーロードを明確に識別するいくつかの方法を示しています。

C#
string phrase = "The quick  brown fox";

_ = phrase.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);

_ = phrase.Split((string[]?)null, StringSplitOptions.RemoveEmptyEntries);

_ = phrase.Split(null as string[], StringSplitOptions.RemoveEmptyEntries);

比較の詳細

Split メソッドは、separator パラメーター内の 1 つ以上の文字列で区切られたこの文字列内の部分文字列を抽出し、それらの部分文字列を配列の要素として返します。

Split メソッドは、大文字と小文字を区別する序数の並べ替え規則を使用して比較を実行することで、区切り記号を検索します。 単語、文字列、序数の並べ替えの詳細については、System.Globalization.CompareOptions 列挙型を参照してください。

Split メソッドは、値が null または空の文字列 ("") である separator の要素を無視します。

separator の文字列に共通の文字がある場合にあいまいな結果を回避するために、Split 操作はインスタンスの値の先頭から末尾に進み、インスタンス内の区切り記号と等しい separator の最初の要素と一致します。 インスタンスで部分文字列が見つかった順序は、separator内の要素の順序よりも優先されます。

たとえば、値が "abcdef" のインスタンスがあるとします。 separator の最初の要素が "ef" で、2 番目の要素が "bcde" の場合、分割操作の結果は、"a" と "f" の 2 つの要素を含む文字列配列になります。 これは、インスタンス "bcde" の部分文字列が検出され、部分文字列 "f" が検出される前に separator 内の要素と一致するためです。

ただし、separator の最初の要素が "bcd" で、2 番目の要素が "bc" の場合、分割操作の結果は、"a" と "ef" の 2 つの要素を含む文字列配列になります。 これは、"bcd" がインスタンス内の区切り記号と一致する separator の最初の区切り記号であるためです。 区切り記号の順序が逆になり、最初の要素が "bc" で、2 番目の要素が "bcd" の場合、結果は "a" と "def" の 2 つの要素を含む文字列配列になります。

パフォーマンスに関する考慮事項

Split メソッドは、返された配列オブジェクトにメモリを割り当て、各配列要素に String オブジェクトを割り当てます。 アプリケーションで最適なパフォーマンスが必要な場合、またはアプリケーションでメモリ割り当ての管理が重要な場合は、IndexOf メソッドまたは IndexOfAny メソッド、および必要に応じて Compare メソッドを使用して、文字列内の部分文字列を検索することを検討してください。

区切り文字で文字列を分割する場合は、IndexOf メソッドまたは IndexOfAny メソッドを使用して、文字列内の区切り文字を検索します。 区切り文字列で文字列を分割する場合は、IndexOf または IndexOfAny メソッドを使用して、区切り文字列の最初の文字を検索します。 次に、Compare メソッドを使用して、最初の文字の後の文字が区切り文字の残りの文字と等しいかどうかを判断します。

さらに、同じ文字セットを使用して複数の Split メソッド呼び出しで文字列を分割する場合は、1 つの配列を作成し、各メソッド呼び出しでそれを参照することを検討してください。 これにより、各メソッド呼び出しの追加オーバーヘッドが大幅に削減されます。

注意 (呼び出し元)

.NET Framework 3.5 以前のバージョンでは、Split(Char[]) メソッドに null または文字が含まれていない separator が渡された場合、このメソッドは、文字列をトリミングするために Trim(Char[]) メソッドとは少し異なる空白文字のセットを使用して文字列を分割します。 .NET Framework 4 以降では、どちらのメソッドも同じ Unicode 空白文字のセットを使用します。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Split(String, StringSplitOptions)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定された文字列区切り記号に基づいて、文字列を部分文字列に分割します。

C#
public string[] Split (string? separator, StringSplitOptions options = System.StringSplitOptions.None);
C#
public string[] Split (string separator, StringSplitOptions options = System.StringSplitOptions.None);

パラメーター

separator
String

この文字列内の部分文字列を区切る文字列。

options
StringSplitOptions

部分文字列をトリミングし、空の部分文字列を含めるかどうかを指定する列挙値のビットごとの組み合わせ。

戻り値

String[]

separatorで区切られた、このインスタンスの部分文字列を要素に含む配列。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Split(Char[])

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した区切り文字に基づいて、文字列を部分文字列に分割します。

C#
public string[] Split (params char[] separator);
C#
public string[] Split (params char[]? separator);

パラメーター

separator
Char[]

区切り文字の配列、区切り記号を含まない空の配列、または null

戻り値

String[]

separator内の 1 つ以上の文字で区切られた、このインスタンスの部分文字列を要素に含む配列。 詳細については、「解説」セクションを参照してください。

次の例では、スペース文字 ( ) とタブ文字 (\t) を区切り記号として扱って、テキスト ブロックから個々の単語を抽出する方法を示します。 分割される文字列には、これらの両方の文字が含まれます。

C#
string s = "Today\tI'm going to school";
string[] subs = s.Split(' ', '\t');

foreach (var sub in subs)
{
    Console.WriteLine($"Substring: {sub}");
}

// This example produces the following output:
//
// Substring: Today
// Substring: I'm
// Substring: going
// Substring: to
// Substring: school

注釈

文字列が既知の文字セットで区切られている場合は、Split(Char[]) メソッドを使用して部分文字列に区切ることができます。

区切り文字は、返される配列の要素には含まれません。 たとえば、区切り記号配列に文字 "-" が含まれており、現在の文字列インスタンスの値が "aa-bb-cc" の場合、メソッドは 3 つの要素 "aa"、"bb"、および "cc" を含む配列を返します。

このインスタンスに separatorの文字が含まれていない場合、返される配列は、このインスタンスを含む 1 つの要素で構成されます。

separator の各要素は、個別の区切り文字を定義します。 2 つの区切り記号が隣接している場合、またはこのインスタンスの先頭または末尾に区切り記号が見つかった場合、返される配列内の対応する要素には Emptyが含まれます。

次の表に、いくつかの例を示します。

言語 文字列値 分離器 返された配列
C# "42, 12, 19" new Char[] {',', ' '} {"42", "", "12", "", "19"}
Visual Basic "42, 12, 19" Char() = {","c, " "c}) {"42", "", "12", "", "19"}
C# "42..12..19." new Char[] {'.'} {"42", "", "12", "", "19", ""}
Visual Basic "42..12..19." Char() = {"."c} {"42", "", "12", "", "19", ""}
C# "Banana" new Char[] {'.'} {"Banana"}
Visual Basic "Banana" Char() = {"."c} {"Banana"}
C# "Darb\nSmarba" new Char[] {} {"Darb", "Smarba"}
Visual Basic "Darb" & vbLf & "Smarba" Char() = {} {"Darb", "Smarba"}
C# "Darb\nSmarba" ヌル {"Darb", "Smarba"}
Visual Basic "Darb" & vbLf & "Smarba" 何もない {"Darb", "Smarba"}

区切り記号の配列

区切り記号の各要素は、1 つの文字で構成される個別の区切り記号を定義します。

separator 引数が null または文字を含まない場合、このメソッドは空白文字を区切り記号として扱います。 空白文字は Unicode 標準で定義され、空白文字が渡された場合、Char.IsWhiteSpace メソッドは true を返します。

String.Split(Char[]) とコンパイラのオーバーロードの解決

String.Split のこのオーバーロードの単一パラメーターは文字配列ですが、次の例に示すように、1 文字で呼び出すことができます。

C#
string value = "This is a short string.";
char delimiter = 's';
string[] substrings = value.Split(delimiter);
foreach (var substring in substrings)
    Console.WriteLine(substring);

// The example displays the following output:
//     Thi
//      i
//      a
//     hort
//     tring.

separator パラメーターは ParamArrayAttribute 属性で修飾されているため、コンパイラは 1 つの文字を単一要素の文字配列として解釈します。 これは、separator パラメーターを含む他の String.Split オーバーロードの場合には当たりません。これらのオーバーロードは、separator 引数として文字配列を明示的に渡す必要があります。

比較の詳細

Split(Char[]) メソッドは、separator 配列内の 1 つ以上の文字で区切られたこの文字列内の部分文字列を抽出し、それらの部分文字列を配列の要素として返します。

Split(Char[]) メソッドは、大文字と小文字を区別する序数の並べ替え規則を使用して比較を実行することで、区切り記号を検索します。 単語、文字列、序数の並べ替えの詳細については、System.Globalization.CompareOptions 列挙型を参照してください。

パフォーマンスに関する考慮事項

Split メソッドは、返された配列オブジェクトにメモリを割り当て、各配列要素に String オブジェクトを割り当てます。 アプリケーションで最適なパフォーマンスが必要な場合、またはアプリケーションでメモリ割り当ての管理が重要な場合は、IndexOf または IndexOfAny メソッドの使用を検討してください。 Compare メソッドを使用して、文字列内の部分文字列を検索することもできます。

区切り文字で文字列を分割するには、IndexOf または IndexOfAny メソッドを使用して、文字列内の区切り文字を検索します。 区切り文字列で文字列を分割するには、IndexOf または IndexOfAny メソッドを使用して、区切り文字列の最初の文字を検索します。 次に、Compare メソッドを使用して、最初の文字の後の文字が区切り文字の残りの文字と等しいかどうかを判断します。

さらに、同じ文字セットを使用して複数の Split メソッド呼び出しで文字列を分割する場合は、1 つの配列を作成し、各メソッド呼び出しでそれを参照することを検討してください。 これにより、各メソッド呼び出しの追加オーバーヘッドが大幅に削減されます。

注意 (呼び出し元)

.NET Framework 3.5 以前のバージョンでは、Split(Char[]) メソッドに null または文字が含まれていない separator が渡された場合、このメソッドは、文字列をトリミングするために Trim(Char[]) メソッドとは少し異なる空白文字のセットを使用して文字列を分割します。 .NET Framework 4 以降では、どちらのメソッドも同じ Unicode 空白文字のセットを使用します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Split(Char[], Int32)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した区切り文字に基づいて、文字列を部分文字列の最大数に分割します。

C#
public string[] Split (char[] separator, int count);
C#
public string[] Split (char[]? separator, int count);

パラメーター

separator
Char[]

この文字列内の部分文字列を区切る文字の配列、区切り記号を含まない空の配列、または null

count
Int32

返される部分文字列の最大数。

戻り値

String[]

separatorの 1 つ以上の文字で区切られた、このインスタンス内の部分文字列を要素に含む配列。 詳細については、「解説」セクションを参照してください。

例外

count は負の値です。

次の例では、count を使用して、Splitによって返される文字列の数を制限する方法を示します。

C#
string name = "Alex Johnson III";

string[] subs = name.Split(null, 2);

string firstName = subs[0];
string lastName;
if (subs.Length > 1)
{
    lastName = subs[1];
}

// firstName = "Alex"
// lastName = "Johnson III"

注釈

区切り文字は、返される配列の要素には含まれません。

このインスタンスに separatorの文字が含まれていない場合、返される配列は、このインスタンスを含む 1 つの要素で構成されます。 count が 0 の場合は、空の配列が返されます。

separator パラメーターが null または文字を含まない場合、空白文字は区切り記号と見なされます。 空白文字は Unicode 標準によって定義され、Char.IsWhiteSpace メソッドは渡された場合に true を返します。

separator の各要素は、個別の区切り文字を定義します。 2 つの区切り記号が隣接している場合、またはこのインスタンスの先頭または末尾に区切り記号が見つかった場合、対応する配列要素には Emptyが含まれます。

このインスタンスに count 以上の部分文字列がある場合は、戻り値の最初の count - 1 要素で最初の count - 1 部分文字列が返され、このインスタンスの残りの文字は戻り値の最後の要素で返されます。

count が部分文字列の数より大きい場合は、使用可能な部分文字列が返され、例外はスローされません。

次の表に、いくつかの例を示します。

言語 文字列値 分離器 返された配列
C# "42, 12, 19" new Char[] {',', ' '} {"42", "", "12", "", "19"}
Visual Basic "42, 12, 19" Char() = {","c, " "c}) {"42", "", "12", "", "19"}
C# "42..12..19." new Char[] {'.'} {"42", "", "12", "", "19", ""}
Visual Basic "42..12..19." Char() = {"."c} {"42", "", "12", "", "19", ""}
C# "Banana" new Char[] {'.'} {"Banana"}
Visual Basic "Banana" Char() = {"."c} {"Banana"}
C# "Darb\nSmarba" new Char[] {} {"Darb", "Smarba"}
Visual Basic "Darb" & vbLf & "Smarba" Char() = {} {"Darb", "Smarba"}
C# "Darb\nSmarba" ヌル {"Darb", "Smarba"}
Visual Basic "Darb" & vbLf & "Smarba" 何もない {"Darb", "Smarba"}

パフォーマンスに関する考慮事項

Split メソッドは、返された配列オブジェクトにメモリを割り当て、各配列要素に String オブジェクトを割り当てます。 アプリケーションで最適なパフォーマンスが必要な場合、またはアプリケーションでメモリ割り当ての管理が重要な場合は、IndexOf メソッドまたは IndexOfAny メソッド、および必要に応じて Compare メソッドを使用して、文字列内の部分文字列を検索することを検討してください。

区切り文字で文字列を分割する場合は、IndexOf メソッドまたは IndexOfAny メソッドを使用して、文字列内の区切り文字を検索します。 区切り文字列で文字列を分割する場合は、IndexOf または IndexOfAny メソッドを使用して、区切り文字列の最初の文字を検索します。 次に、Compare メソッドを使用して、最初の文字の後の文字が区切り文字の残りの文字と等しいかどうかを判断します。

さらに、同じ文字セットを使用して複数の Split メソッド呼び出しで文字列を分割する場合は、1 つの配列を作成し、各メソッド呼び出しでそれを参照することを検討してください。 これにより、各メソッド呼び出しの追加オーバーヘッドが大幅に削減されます。

注意 (呼び出し元)

.NET Framework 3.5 以前のバージョンでは、Split(Char[]) メソッドに null または文字が含まれていない separator が渡された場合、このメソッドは、文字列をトリミングするために Trim(Char[]) メソッドとは少し異なる空白文字のセットを使用して文字列を分割します。 .NET Framework 4 以降では、どちらのメソッドも同じ Unicode 空白文字のセットを使用します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Split(Char, StringSplitOptions)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した区切り文字とオプション (オプション) に基づいて、文字列を部分文字列に分割します。

C#
public string[] Split (char separator, StringSplitOptions options = System.StringSplitOptions.None);

パラメーター

separator
Char

この文字列内の部分文字列を区切る文字。

options
StringSplitOptions

部分文字列をトリミングし、空の部分文字列を含めるかどうかを指定する列挙値のビットごとの組み合わせ。

戻り値

String[]

separatorで区切られた、このインスタンスの部分文字列を要素に含む配列。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Split(ReadOnlySpan<Char>)

指定した区切り文字に基づいて、文字列を部分文字列に分割します。

C#
public string[] Split (scoped ReadOnlySpan<char> separator);

パラメーター

separator
ReadOnlySpan<Char>

区切り文字のスパン、または区切り記号を含まない空のスパン。

戻り値

String[]

separator内の 1 つ以上の文字で区切られた、このインスタンスの部分文字列を要素に含む配列。

適用対象

.NET 9
製品 バージョン
.NET 9

Split(String[], Int32, StringSplitOptions)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した区切り文字列とオプションに基づいて、文字列を部分文字列の最大数に分割します。

C#
public string[] Split (string[] separator, int count, StringSplitOptions options);
C#
public string[] Split (string[]? separator, int count, StringSplitOptions options);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public string[] Split (string[] separator, int count, StringSplitOptions options);

パラメーター

separator
String[]

この文字列内の部分文字列を区切る文字列、区切り記号を含まない空の配列、または null

count
Int32

返される部分文字列の最大数。

options
StringSplitOptions

部分文字列をトリミングし、空の部分文字列を含めるかどうかを指定する列挙値のビットごとの組み合わせ。

戻り値

String[]

separator内の 1 つ以上の文字列で区切られた、この文字列内の部分文字列を要素に含む配列。 詳細については、「解説」セクションを参照してください。

属性

例外

count は負の値です。

options は、StringSplitOptions 値の 1 つではありません。

次の例では、StringSplitOptions 列挙型を使用して、Split メソッドによって生成された部分文字列を含めるか除外します。

C#
// This example demonstrates the String.Split() methods that use
// the StringSplitOptions enumeration.

// Example 1: Split a string delimited by characters
Console.WriteLine("1) Split a string delimited by characters:\n");

string s1 = ",ONE,, TWO,, , THREE,,";
char[] charSeparators = new char[] { ',' };
string[] result;

Console.WriteLine($"The original string is: \"{s1}\".");
Console.WriteLine($"The delimiter character is: '{charSeparators[0]}'.\n");

// Split the string and return all elements
Console.WriteLine("1a) Return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);

// Split the string and return all elements with whitespace trimmed
Console.WriteLine("1b) Return all elements with whitespace trimmed:");
result = s1.Split(charSeparators, StringSplitOptions.TrimEntries);
Show(result);

// Split the string and return all non-empty elements
Console.WriteLine("1c) Return all non-empty elements:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string and return all non-whitespace elements with whitespace trimmed
Console.WriteLine("1d) Return all non-whitespace elements with whitespace trimmed:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Split the string into only two elements, keeping the remainder in the last match
Console.WriteLine("1e) Split into only two elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);

// Split the string into only two elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("1f) Split into only two elements with whitespace trimmed:");
result = s1.Split(charSeparators, 2, StringSplitOptions.TrimEntries);
Show(result);

// Split the string into only two non-empty elements, keeping the remainder in the last match
Console.WriteLine("1g) Split into only two non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string into only two non-whitespace elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("1h) Split into only two non-whitespace elements with whitespace trimmed:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Example 2: Split a string delimited by another string
Console.WriteLine("2) Split a string delimited by another string:\n");

string s2 = "[stop]" +
            "ONE[stop] [stop]" +
            "TWO  [stop][stop]  [stop]" +
            "THREE[stop][stop]  ";
string[] stringSeparators = new string[] { "[stop]" };

Console.WriteLine($"The original string is: \"{s2}\".");
Console.WriteLine($"The delimiter string is: \"{stringSeparators[0]}\".\n");

// Split the string and return all elements
Console.WriteLine("2a) Return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result);

// Split the string and return all elements with whitespace trimmed
Console.WriteLine("2b) Return all elements with whitespace trimmed:");
result = s2.Split(stringSeparators, StringSplitOptions.TrimEntries);
Show(result);

// Split the string and return all non-empty elements
Console.WriteLine("2c) Return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string and return all non-whitespace elements with whitespace trimmed
Console.WriteLine("2d) Return all non-whitespace elements with whitespace trimmed:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Split the string into only two elements, keeping the remainder in the last match
Console.WriteLine("2e) Split into only two elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result);

// Split the string into only two elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("2f) Split into only two elements with whitespace trimmed:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.TrimEntries);
Show(result);

// Split the string into only two non-empty elements, keeping the remainder in the last match
Console.WriteLine("2g) Split into only two non-empty elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string into only two non-whitespace elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("2h) Split into only two non-whitespace elements with whitespace trimmed:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Display the array of separated strings using a local function
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");
}

/*
This example produces the following results:

1) Split a string delimited by characters:

The original string is: ",ONE,, TWO,, , THREE,,".
The delimiter character is: ','.

1a) Return all elements:
The return value contains these 9 elements:
<><ONE><>< TWO><>< >< THREE><><>

1b) Return all elements with whitespace trimmed:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

1c) Return all non-empty elements:
The return value contains these 4 elements:
<ONE>< TWO>< >< THREE>

1d) Return all non-whitespace elements with whitespace trimmed:
The return value contains these 3 elements:
<ONE><TWO><THREE>

1e) Split into only two elements:
The return value contains these 2 elements:
<><ONE,, TWO,, , THREE,,>

1f) Split into only two elements with whitespace trimmed:
The return value contains these 2 elements:
<><ONE,, TWO,, , THREE,,>

1g) Split into only two non-empty elements:
The return value contains these 2 elements:
<ONE>< TWO,, , THREE,,>

1h) Split into only two non-whitespace elements with whitespace trimmed:
The return value contains these 2 elements:
<ONE><TWO,, , THREE,,>

2) Split a string delimited by another string:

The original string is: "[stop]ONE[stop] [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]  ".
The delimiter string is: "[stop]".

2a) Return all elements:
The return value contains these 9 elements:
<><ONE>< ><TWO  ><><  ><THREE><><  >

2b) Return all elements with whitespace trimmed:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

2c) Return all non-empty elements:
The return value contains these 6 elements:
<ONE>< ><TWO  ><  ><THREE><  >

2d) Return all non-whitespace elements with whitespace trimmed:
The return value contains these 3 elements:
<ONE><TWO><THREE>

2e) Split into only two elements:
The return value contains these 2 elements:
<><ONE[stop] [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]  >

2f) Split into only two elements with whitespace trimmed:
The return value contains these 2 elements:
<><ONE[stop] [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]>

2g) Split into only two non-empty elements:
The return value contains these 2 elements:
<ONE>< [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]  >

2h) Split into only two non-whitespace elements with whitespace trimmed:
The return value contains these 2 elements:
<ONE><TWO  [stop][stop]  [stop]THREE[stop][stop]>

*/

注釈

区切り記号文字列は、返される配列の要素には含まれません。

このインスタンスに separator内の文字列が含まれていない場合、または count パラメーターが 1 の場合、返される配列は、このインスタンスを含む 1 つの要素で構成されます。

separator パラメーターが null または文字を含まない場合、空白文字は区切り記号と見なされます。 空白文字は Unicode 標準によって定義され、Char.IsWhiteSpace メソッドは渡された場合に true を返します。

string[] separator パラメーターの null を渡すには、null の型を指定して、Split(Char[], Int32, StringSplitOptions)などの他のいくつかのオーバーロードからの呼び出しを明確にする必要があります。 次の例は、このオーバーロードを明確に識別するいくつかの方法を示しています。

C#
string phrase = "The quick  brown fox";

_ = phrase.Split(default(string[]), 3, StringSplitOptions.RemoveEmptyEntries);

_ = phrase.Split((string[]?)null, 3, StringSplitOptions.RemoveEmptyEntries);

_ = phrase.Split(null as string[], 3, StringSplitOptions.RemoveEmptyEntries);

count パラメーターが 0 の場合、または options パラメーターが RemoveEmptyEntries で、このインスタンスの長さが 0 の場合は、空の配列が返されます。

separator の各要素は、1 つ以上の文字で構成される個別の区切り記号を定義します。 options パラメーターが Noneされ、2 つの区切り記号が隣接している場合、またはこのインスタンスの先頭または末尾に区切り記号が見つかった場合、対応する配列要素には Emptyが含まれます。

このインスタンスに count を超える部分文字列がある場合、最初の count から 1 個の部分文字列を引いた部分文字列が戻り値の最初の count から 1 要素を引いた値で返され、このインスタンスの残りの文字は戻り値の最後の要素で返されます。

count が部分文字列の数より大きい場合は、使用可能な部分文字列が返され、例外はスローされません。

区切り記号の配列

separator のいずれかの要素が複数の文字で構成されている場合、部分文字列全体は区切り記号と見なされます。 たとえば、separator 内のいずれかの要素が "10" の場合、文字列 "This10is10a10string" を分割しようとすると、次の 4 要素配列 {"This"、"is"、"a"、"string." }が返されます。

比較の詳細

Split メソッドは、separator パラメーター内の 1 つ以上の文字列で区切られたこの文字列内の部分文字列を抽出し、それらの部分文字列を配列の要素として返します。

Split メソッドは、大文字と小文字を区別する序数の並べ替え規則を使用して比較を実行することで、区切り記号を検索します。 単語、文字列、序数の並べ替えの詳細については、System.Globalization.CompareOptions 列挙型を参照してください。

Split メソッドは、値が null または空の文字列 ("") である separator の要素を無視します。

separator の文字列に共通の文字がある場合にあいまいな結果を回避するために、Split メソッドはインスタンスの値の先頭から末尾に進み、インスタンス内の区切り記号と等しい separator の最初の要素と一致します。 インスタンスで部分文字列が見つかった順序は、separator内の要素の順序よりも優先されます。

たとえば、値が "abcdef" のインスタンスがあるとします。 separator の最初の要素が "ef" で、2 番目の要素が "bcde" の場合、分割操作の結果は "a" と "f" になります。 これは、インスタンス "bcde" の部分文字列が検出され、部分文字列 "f" が検出される前に separator 内の要素と一致するためです。

ただし、separator の最初の要素が "bcd" で、2 番目の要素が "bc" の場合、分割操作の結果は "a" と "ef" になります。 これは、"bcd" がインスタンス内の区切り記号と一致する separator の最初の区切り記号であるためです。 区切り記号の順序が逆になり、最初の要素が "bc" で、2 番目の要素が "bcd" の場合、結果は "a" と "def" になります。

パフォーマンスに関する考慮事項

Split メソッドは、返された配列オブジェクトにメモリを割り当て、各配列要素に String オブジェクトを割り当てます。 アプリケーションで最適なパフォーマンスが必要な場合、またはアプリケーションでメモリ割り当ての管理が重要な場合は、IndexOf メソッドまたは IndexOfAny メソッド、および必要に応じて Compare メソッドを使用して、文字列内の部分文字列を検索することを検討してください。

区切り文字で文字列を分割する場合は、IndexOf メソッドまたは IndexOfAny メソッドを使用して、文字列内の区切り文字を検索します。 区切り文字列で文字列を分割する場合は、IndexOf または IndexOfAny メソッドを使用して、区切り文字列の最初の文字を検索します。 次に、Compare メソッドを使用して、最初の文字の後の文字が区切り文字の残りの文字と等しいかどうかを判断します。

さらに、同じ文字セットを使用して複数の Split メソッド呼び出しで文字列を分割する場合は、1 つの配列を作成し、各メソッド呼び出しでそれを参照することを検討してください。 これにより、各メソッド呼び出しの追加オーバーヘッドが大幅に削減されます。

注意 (呼び出し元)

.NET Framework 3.5 以前のバージョンでは、Split(Char[]) メソッドに null または文字が含まれていない separator が渡された場合、このメソッドは、文字列をトリミングするために Trim(Char[]) メソッドとは少し異なる空白文字のセットを使用して文字列を分割します。 .NET Framework 4 以降では、どちらのメソッドも同じ Unicode 空白文字のセットを使用します。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Split(Char[], StringSplitOptions)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した区切り文字とオプションに基づいて、文字列を部分文字列に分割します。

C#
public string[] Split (char[] separator, StringSplitOptions options);
C#
public string[] Split (char[]? separator, StringSplitOptions options);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public string[] Split (char[] separator, StringSplitOptions options);

パラメーター

separator
Char[]

この文字列内の部分文字列を区切る文字の配列、区切り記号を含まない空の配列、または null

options
StringSplitOptions

部分文字列をトリミングし、空の部分文字列を含めるかどうかを指定する列挙値のビットごとの組み合わせ。

戻り値

String[]

separator内の 1 つ以上の文字で区切られた、この文字列内の部分文字列を要素に含む配列。 詳細については、「解説」セクションを参照してください。

属性

例外

options は、StringSplitOptions 値の 1 つではありません。

次の例では、StringSplitOptions 列挙型を使用して、Split メソッドによって生成された部分文字列を含めるか除外します。

C#
// This example demonstrates the String.Split() methods that use
// the StringSplitOptions enumeration.

// Example 1: Split a string delimited by characters
Console.WriteLine("1) Split a string delimited by characters:\n");

string s1 = ",ONE,, TWO,, , THREE,,";
char[] charSeparators = new char[] { ',' };
string[] result;

Console.WriteLine($"The original string is: \"{s1}\".");
Console.WriteLine($"The delimiter character is: '{charSeparators[0]}'.\n");

// Split the string and return all elements
Console.WriteLine("1a) Return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);

// Split the string and return all elements with whitespace trimmed
Console.WriteLine("1b) Return all elements with whitespace trimmed:");
result = s1.Split(charSeparators, StringSplitOptions.TrimEntries);
Show(result);

// Split the string and return all non-empty elements
Console.WriteLine("1c) Return all non-empty elements:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string and return all non-whitespace elements with whitespace trimmed
Console.WriteLine("1d) Return all non-whitespace elements with whitespace trimmed:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Split the string into only two elements, keeping the remainder in the last match
Console.WriteLine("1e) Split into only two elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);

// Split the string into only two elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("1f) Split into only two elements with whitespace trimmed:");
result = s1.Split(charSeparators, 2, StringSplitOptions.TrimEntries);
Show(result);

// Split the string into only two non-empty elements, keeping the remainder in the last match
Console.WriteLine("1g) Split into only two non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string into only two non-whitespace elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("1h) Split into only two non-whitespace elements with whitespace trimmed:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Example 2: Split a string delimited by another string
Console.WriteLine("2) Split a string delimited by another string:\n");

string s2 = "[stop]" +
            "ONE[stop] [stop]" +
            "TWO  [stop][stop]  [stop]" +
            "THREE[stop][stop]  ";
string[] stringSeparators = new string[] { "[stop]" };

Console.WriteLine($"The original string is: \"{s2}\".");
Console.WriteLine($"The delimiter string is: \"{stringSeparators[0]}\".\n");

// Split the string and return all elements
Console.WriteLine("2a) Return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result);

// Split the string and return all elements with whitespace trimmed
Console.WriteLine("2b) Return all elements with whitespace trimmed:");
result = s2.Split(stringSeparators, StringSplitOptions.TrimEntries);
Show(result);

// Split the string and return all non-empty elements
Console.WriteLine("2c) Return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string and return all non-whitespace elements with whitespace trimmed
Console.WriteLine("2d) Return all non-whitespace elements with whitespace trimmed:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Split the string into only two elements, keeping the remainder in the last match
Console.WriteLine("2e) Split into only two elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result);

// Split the string into only two elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("2f) Split into only two elements with whitespace trimmed:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.TrimEntries);
Show(result);

// Split the string into only two non-empty elements, keeping the remainder in the last match
Console.WriteLine("2g) Split into only two non-empty elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);

// Split the string into only two non-whitespace elements with whitespace trimmed, keeping the remainder in the last match
Console.WriteLine("2h) Split into only two non-whitespace elements with whitespace trimmed:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Show(result);


// Display the array of separated strings using a local function
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");
}

/*
This example produces the following results:

1) Split a string delimited by characters:

The original string is: ",ONE,, TWO,, , THREE,,".
The delimiter character is: ','.

1a) Return all elements:
The return value contains these 9 elements:
<><ONE><>< TWO><>< >< THREE><><>

1b) Return all elements with whitespace trimmed:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

1c) Return all non-empty elements:
The return value contains these 4 elements:
<ONE>< TWO>< >< THREE>

1d) Return all non-whitespace elements with whitespace trimmed:
The return value contains these 3 elements:
<ONE><TWO><THREE>

1e) Split into only two elements:
The return value contains these 2 elements:
<><ONE,, TWO,, , THREE,,>

1f) Split into only two elements with whitespace trimmed:
The return value contains these 2 elements:
<><ONE,, TWO,, , THREE,,>

1g) Split into only two non-empty elements:
The return value contains these 2 elements:
<ONE>< TWO,, , THREE,,>

1h) Split into only two non-whitespace elements with whitespace trimmed:
The return value contains these 2 elements:
<ONE><TWO,, , THREE,,>

2) Split a string delimited by another string:

The original string is: "[stop]ONE[stop] [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]  ".
The delimiter string is: "[stop]".

2a) Return all elements:
The return value contains these 9 elements:
<><ONE>< ><TWO  ><><  ><THREE><><  >

2b) Return all elements with whitespace trimmed:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

2c) Return all non-empty elements:
The return value contains these 6 elements:
<ONE>< ><TWO  ><  ><THREE><  >

2d) Return all non-whitespace elements with whitespace trimmed:
The return value contains these 3 elements:
<ONE><TWO><THREE>

2e) Split into only two elements:
The return value contains these 2 elements:
<><ONE[stop] [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]  >

2f) Split into only two elements with whitespace trimmed:
The return value contains these 2 elements:
<><ONE[stop] [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]>

2g) Split into only two non-empty elements:
The return value contains these 2 elements:
<ONE>< [stop]TWO  [stop][stop]  [stop]THREE[stop][stop]  >

2h) Split into only two non-whitespace elements with whitespace trimmed:
The return value contains these 2 elements:
<ONE><TWO  [stop][stop]  [stop]THREE[stop][stop]>

*/

注釈

返される配列の要素には、区切り文字 (separator 配列内の文字) は含まれません。 たとえば、separator 配列に文字 "-" が含まれており、現在の文字列インスタンスの値が "aa-bb-cc" の場合、メソッドは 3 つの要素 "aa"、"bb"、および "cc" を含む配列を返します。

このインスタンスに separatorの文字が含まれていない場合、返される配列は、このインスタンスを含む 1 つの要素で構成されます。

options パラメーターが RemoveEmptyEntries で、このインスタンスの長さが 0 の場合、このメソッドは空の配列を返します。

separator の各要素は、1 つの文字で構成される個別の区切り記号を定義します。 options 引数が Noneされ、2 つの区切り記号が隣接している場合、またはこのインスタンスの先頭または末尾に区切り記号が見つかった場合は、対応する配列要素に String.Emptyが含まれます。 たとえば、separator'-''_'の 2 つの要素が含まれている場合、文字列インスタンスの値は "-_aa-_" で、options 引数の値が None場合、メソッドは次の 5 つの要素を持つ文字列配列を返します。

  1. String.Empty:インデックス 0 の "-" 文字の前にある空の文字列を表します。

  2. String.Empty:インデックス 0 の "-" 文字とインデックス 1 の "_" 文字の間の空の文字列を表します。

  3. "aa"

  4. String.Empty:インデックス 4 の "-" 文字の後に続く空の文字列を表します。

  5. String.Empty:インデックス 5 の "_" 文字の後に続く空の文字列を表します。

区切り記号の配列

separator パラメーターが null または文字を含まない場合、空白文字は区切り記号と見なされます。 空白文字は Unicode 標準によって定義され、Char.IsWhiteSpace メソッドは渡された場合に true を返します。

char[] separator パラメーターの null を渡すには、null の型を指定して、Split(String[], StringSplitOptions)などの他のいくつかのオーバーロードからの呼び出しを明確にする必要があります。 次の例は、このオーバーロードを明確に識別するいくつかの方法を示しています。

C#
string phrase = "The quick  brown fox";

_ = phrase.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);

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

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

比較の詳細

Split メソッドは、separator パラメーター内の 1 つ以上の文字で区切られたこの文字列内の部分文字列を抽出し、それらの部分文字列を配列の要素として返します。

Split メソッドは、大文字と小文字を区別する序数の並べ替え規則を使用して比較を実行することで、区切り記号を検索します。 単語、文字列、序数の並べ替えの詳細については、System.Globalization.CompareOptions 列挙型を参照してください。

パフォーマンスに関する考慮事項

Split メソッドは、返された配列オブジェクトにメモリを割り当て、各配列要素に String オブジェクトを割り当てます。 アプリケーションで最適なパフォーマンスが必要な場合、またはアプリケーションでメモリ割り当ての管理が重要な場合は、IndexOf メソッドまたは IndexOfAny メソッド、および必要に応じて Compare メソッドを使用して、文字列内の部分文字列を検索することを検討してください。

区切り文字で文字列を分割する場合は、IndexOf メソッドまたは IndexOfAny メソッドを使用して、文字列内の区切り文字を検索します。 区切り文字列で文字列を分割する場合は、IndexOf または IndexOfAny メソッドを使用して、区切り文字列の最初の文字を検索します。 次に、Compare メソッドを使用して、最初の文字の後の文字が区切り文字の残りの文字と等しいかどうかを判断します。

さらに、同じ文字セットを使用して複数の Split メソッド呼び出しで文字列を分割する場合は、1 つの配列を作成し、各メソッド呼び出しでそれを参照することを検討してください。 これにより、各メソッド呼び出しの追加オーバーヘッドが大幅に削減されます。

注意 (呼び出し元)

.NET Framework 3.5 以前のバージョンでは、Split(Char[]) メソッドに null または文字が含まれていない separator が渡された場合、このメソッドは、文字列をトリミングするために Trim(Char[]) メソッドとは少し異なる空白文字のセットを使用して文字列を分割します。 .NET Framework 4 以降では、どちらのメソッドも同じ Unicode 空白文字のセットを使用します。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0