String.Split メソッド (String[], Int32, StringSplitOptions)
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(False)> _ Public Function Split ( _ separator As String(), _ count As Integer, _ options As StringSplitOptions _ ) As String()
Dim instance As String Dim separator As String() Dim count As Integer Dim options As StringSplitOptions Dim returnValue As String() returnValue = instance.Split(separator, count, options)
[ComVisibleAttribute(false)] public string[] Split ( string[] separator, int count, StringSplitOptions options )
[ComVisibleAttribute(false)] public: array<String^>^ Split ( array<String^>^ separator, int count, StringSplitOptions options )
/** @attribute ComVisibleAttribute(false) */ public String[] Split ( String[] separator, int count, StringSplitOptions options )
ComVisibleAttribute(false) public function Split ( separator : String[], count : int, options : StringSplitOptions ) : String[]
- separator
この文字列から部分文字列を取り出すために区切り文字として使用する文字列の配列、区切り文字が含まれていない空の配列、または null 参照 (Visual Basic では Nothing)。
この文字列を、separator 配列のいずれかまたは複数の要素 (文字列) で区切ることによって取り出された部分文字列を格納する配列。詳細については、「解説」を参照してください。


戻り値の配列要素に区切り記号文字列は含まれません。区切り記号は、序数に基づく比較を使用して検出されます。
このインスタンスに、separator に指定されたいずれの文字列も含まれていなかった場合、戻り値の配列には、そのインスタンス自体を保持する要素が 1 つだけ格納されます。separator パラメータが null 参照 (Visual Basic では Nothing) の場合または文字が含まれていなかった場合、区切り記号に空白文字が指定されたものとして実行されます。
count パラメータがゼロの場合、または、options パラメータが RemoveEmptyEntries で、このインスタンスの長さがゼロの場合、空の配列が返されます。
options パラメータが Noneで、2 つの区切り記号が隣接している場合、または、区切り記号がこのインスタンスの先頭か末尾に見つかった場合、対応する配列の要素には Empty が格納されます。
このインスタンスに count を超える部分文字列が含まれていた場合、count - 1 番目にある最初の部分文字列が、戻り値の count - 1 番目の最初の要素として返されます。このインスタンスに含まれるそれ以外の文字は、戻り値の最後の要素として返されます。
Split メソッドは、この文字列を、separator パラメータで指定された 1 つまたは複数の文字列で区切ることによって部分文字列を抽出し、これらの部分文字列を配列の要素として返します。
Split メソッドが区切り記号を検索する際の比較処理では、大文字と小文字は区別され、序数の並べ替え規則が使用されます。単語、文字列、序数の並べ替えの詳細については、System.Globalization.CompareOptions 列挙値のトピックを参照してください。
Split メソッドでは、値が null 参照 (Visual Basic では Nothing) または空の文字列 ("") である separator の要素は無視されます。
separator に格納された複数の文字列に共通の文字が使用されていた場合にあいまいな結果が生じるのを防ぐため、Split メソッドは、対象のインスタンスの値を先頭から最後まで解析し、separator 内の要素と最初に一致した要素を区切り文字として使用します。インスタンスにおける部分文字列の出現順が、separator における要素の格納順よりも優先されます。
たとえば、値が "abcdef" であるインスタンスがあるとします。separator の 1 つ目の要素が "ef"、2 つ目の要素が "bcde" であった場合、分割処理の結果は "a" と "f" になります。インスタンス内の部分文字列 "bcde" が、部分文字列 "f" よりも先に separator 内の要素と一致するためです。
ただし、separator の 1 つ目の要素が "bcd" で、2 つ目の要素が "bc" であった場合、分割処理の結果は "a" と "ef" になります。これは、"bcd" が、separator に格納された要素のうち、インスタンス内の区切り記号と一致する最初の要素であるためです。separator に格納された要素の順序が逆だった場合、つまり、1 つ目の要素が "bc" で、2 つ目の要素が "bcd" であった場合、分割処理の結果は "a" と "def" となります。

Split メソッドによって生成される部分文字列を結果に含めるか除外するかを StringSplitOptions 列挙値を使って指定するコード例を次に示します。
' This example demonstrates the String() methods that use ' the StringSplitOptions enumeration. Imports System Class Sample Public Shared Sub Main() Dim s1 As String = ",ONE,,TWO,,,THREE,," Dim s2 As String = "[stop]" & _ "ONE[stop][stop]" & _ "TWO[stop][stop][stop]" & _ "THREE[stop][stop]" Dim charSeparators() As Char = {","c} Dim stringSeparators() As String = {"[stop]"} Dim result() As String ' ------------------------------------------------------------------------------ ' Split a string delimited by characters. ' ------------------------------------------------------------------------------ Console.WriteLine("1) Split a string delimited by characters:" & vbCrLf) ' Display the original string and delimiter characters. Console.WriteLine("1a )The original string is ""{0}"".", s1) Console.WriteLine("The delimiter character is '{0}'." & vbCrLf, 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:" & vbCrLf) ' Display the original string and delimiter string. Console.WriteLine("2a) The original string is ""{0}"".", s2) Console.WriteLine("The delimiter string is ""{0}""." & vbCrLf, 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) End Sub 'Main ' Display the array of separated strings. Public Shared Sub Show(ByVal entries() As String) Console.WriteLine("The return value contains these {0} elements:", entries.Length) Dim entry As String For Each entry In entries Console.Write("<{0}>", entry) Next entry Console.Write(vbCrLf & vbCrLf) End Sub 'Show End Class 'Sample ' '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]> '
// 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 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Split メソッド (Char[])
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As String Dim separator As Char() Dim returnValue As String() returnValue = instance.Split(separator)
- separator
このインスタンスでの部分文字列の区切り文字である Unicode 文字の配列、区切り文字が含まれていない空の配列、または null 参照 (Visual Basic では Nothing)。
このインスタンスを、separator 配列のいずれかまたは複数の要素 (文字) で区切ることによって取り出された部分文字列を格納する配列。詳細については、「解説」を参照してください。

戻り値の配列要素に区切り記号文字は含まれません。区切り記号は、序数に基づく比較を使用して検出されます。
このインスタンスに、separator に指定されたいずれの文字も含まれていなかった場合、戻り値の配列には、そのインスタンス自体を保持する要素が 1 つだけ格納されます。separator パラメータが null 参照 (Visual Basic では Nothing) の場合または文字が含まれていなかった場合、区切り記号に空白文字が指定されたものとして実行されます。
2 つの区切り記号が隣接している場合、または、区切り記号がこのインスタンスの先頭か末尾に見つかった場合、対応する配列の要素には Empty が格納されます。
"42..12..19" | ||
"Banana" | {"Banana"} | |
"Darb\nSmarba" | {"Darb", "Smarba"} | |
"Darb\nSmarba" | {"Darb", "Smarba"} |
パフォーマンスに関する考慮事項
Split メソッドでは、戻り値の配列オブジェクト用と、各配列要素の String オブジェクト用にメモリが確保されます。アプリケーション開発においてメモリ割り当ての管理がきわめて重要な要件となる場合は、IndexOfAny メソッドと、(必要に応じて) Compare メソッドを使用して文字列から部分文字列を検索します。
区切り記号文字の位置で文字列を分割する場合は、IndexOfAny メソッドを使用して、文字列中の区切り記号文字を検索します。区切り記号文字の位置で文字列を分割する場合は、IndexOfAny メソッドを使用して、区切り記号文字の最初の文字を検索します。次に、Compare メソッドを使用して、最初の文字に続く文字が区切り記号文字の残りの文字に等しいかどうかを判断します。

Split メソッドを使用して、文字列をトークン化する方法については、次のコード例を参照してください。
Imports System Public Class SplitTest Public Shared Sub Main() Dim words As String = "this is a list of words, with: a bit of punctuation." Dim split As String() = words.Split(New [Char]() {" "c, ","c, "."c, ":"c}) Dim s As String For Each s In split If s.Trim() <> "" Then Console.WriteLine(s) End If Next s End Sub 'Main End Class 'SplitTest
using System; public class SplitTest { public static void Main() { string words = "this is a list of words, with: a bit of punctuation."; string [] split = words.Split(new Char [] {' ', ',', '.', ':'}); foreach (string s in split) { if (s.Trim() != "") Console.WriteLine(s); } } }
using namespace System; using namespace System::Collections; int main() { String^ words = "this is a list of words, with: a bit of punctuation."; array<Char>^chars = {' ',',','->',':'}; array<String^>^split = words->Split( chars ); IEnumerator^ myEnum = split->GetEnumerator(); while ( myEnum->MoveNext() ) { String^ s = safe_cast<String^>(myEnum->Current); if ( !s->Trim()->Equals( "" ) ) Console::WriteLine( s ); } }
import System.*; public class SplitTest { public static void main(String[] args) { String words = "this is a list of words, with: a bit of punctuation."; String split[] = words.Split(new char[] { ' ', ',', '.', ':' }); for (int iCtr = 0; iCtr < split.get_Length(); iCtr++) { String s = (String)split.get_Item(iCtr); if (!(s.Trim().Equals(""))) { Console.WriteLine(s); } } } //main } //SplitTest
import System; public class SplitTest { public static function Main() : void { var words : String = "this is a list of words, with: a bit of punctuation."; var separators : char[] = [' ', ',', '.', ':']; var split : String [] = words.Split(separators); for (var i : int in split) { var s : String = split[i]; if (s.Trim() != "") Console.WriteLine(s); } } } SplitTest.Main();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Split メソッド (Char[], Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As String Dim separator As Char() Dim count As Integer Dim returnValue As String() returnValue = instance.Split(separator, count)
- separator
このインスタンスでの部分文字列の区切り文字である Unicode 文字の配列、区切り文字が含まれていない空の配列、または null 参照 (Visual Basic では Nothing)。
このインスタンスを、separator 配列のいずれかまたは複数の要素 (文字) で区切ることによって取り出された部分文字列を格納する配列。詳細については、「解説」を参照してください。


戻り値の配列要素に区切り記号文字は含まれません。区切り記号は、序数に基づく比較を使用して検出されます。
このインスタンスに、separator に指定されたいずれの文字も含まれていなかった場合、戻り値の配列には、そのインスタンス自体を保持する要素が 1 つだけ格納されます。separator パラメータが null 参照 (Visual Basic では Nothing) の場合または文字が含まれていなかった場合、区切り記号に空白文字が指定されたものとして実行されます。count がゼロの場合、空の配列が返されます。
2 つの区切り記号が隣接している場合、または、区切り記号がこのインスタンスの先頭か末尾に見つかった場合、対応する配列の要素には Empty が格納されます。
このインスタンスに count を超える部分文字列が含まれていた場合、count - 1 番目にある最初の部分文字列が、戻り値の count - 1 番目の最初の要素として返されます。このインスタンスに含まれるそれ以外の文字は、戻り値の最後の要素として返されます。
count が部分文字列の数よりも多い場合、取り出せるだけの部分文字列が返され、例外はスローされません。

count が、Split によって返される文字列の数に与える影響については、次のコード例を参照してください。
Imports System Imports Microsoft.VisualBasic _ Public Class StringSplit2 Public Shared Sub Main() Dim delimStr As String = " ,.:" Dim delimiter As Char() = delimStr.ToCharArray() Dim words As String = "one two,three:four." Dim split As String() = Nothing Console.WriteLine("The delimiters are -{0}-", delimStr) Dim x As Integer For x = 1 To 5 split = words.Split(delimiter, x) Console.WriteLine(ControlChars.Cr + "count = {0,2} ..............", x) Dim s As String For Each s In split Console.WriteLine("-{0}-", s) Next s Next x End Sub 'Main End Class 'StringSplit2
using System; public class StringSplit2 { public static void Main() { string delimStr = " ,.:"; char [] delimiter = delimStr.ToCharArray(); string words = "one two,three:four."; string [] split = null; Console.WriteLine("The delimiters are -{0}-", delimStr); for (int x = 1; x <= 5; x++) { split = words.Split(delimiter, x); Console.WriteLine("\ncount = {0,2} ..............", x); foreach (string s in split) { Console.WriteLine("-{0}-", s); } } } }
using namespace System; using namespace System::Collections; int main() { String^ delimStr = " ,.:"; array<Char>^delimiter = delimStr->ToCharArray(); String^ words = "one two,three:four."; array<String^>^split = nullptr; Console::WriteLine( "The delimiters are -{0}-", delimStr ); for ( int x = 1; x <= 5; x++ ) { split = words->Split( delimiter, x ); Console::WriteLine( "\ncount = {0, 2} ..............", x ); IEnumerator^ myEnum = split->GetEnumerator(); while ( myEnum->MoveNext() ) { String^ s = safe_cast<String^>(myEnum->Current); Console::WriteLine( "-{0}-", s ); } } }
import System.*; public class StringSplit2 { public static void main(String[] args) { String delimStr = " ,.:"; char delimiter[] = delimStr.ToCharArray(); String words = "one two,three:four."; String split[] = null; Console.WriteLine("The delimiters are -{0}-", delimStr); for (int x = 1; x <= 5; x++) { split = words.Split(delimiter, x); Console.WriteLine("\ncount = {0,2} ..............", System.Convert.ToString(x)); for (int iCtr = 0; iCtr < split.get_Length(); iCtr++) { String s = split[iCtr]; Console.WriteLine("-{0}-", s); } } } //main } //StringSplit2

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Split メソッド (Char[], StringSplitOptions)
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(False)> _ Public Function Split ( _ separator As Char(), _ options As StringSplitOptions _ ) As String()
Dim instance As String Dim separator As Char() Dim options As StringSplitOptions Dim returnValue As String() returnValue = instance.Split(separator, options)
[ComVisibleAttribute(false)] public: array<String^>^ Split ( array<wchar_t>^ separator, StringSplitOptions options )
/** @attribute ComVisibleAttribute(false) */ public String[] Split ( char[] separator, StringSplitOptions options )
ComVisibleAttribute(false) public function Split ( separator : char[], options : StringSplitOptions ) : String[]
- separator
この文字列から部分文字列を取り出すために区切り文字として使用する Unicode 文字の配列、区切り文字が含まれていない空の配列、または null 参照 (Visual Basic では Nothing)。
この文字列を、separator 配列のいずれかまたは複数の要素 (文字) で区切ることによって取り出された部分文字列を格納する配列。詳細については、「解説」を参照してください。


戻り値の配列要素に区切り記号文字は含まれません。区切り記号は、序数に基づく比較を使用して検出されます。
このインスタンスに、separator に指定されたいずれの文字も含まれていなかった場合、戻り値の配列には、そのインスタンス自体を保持する要素が 1 つだけ格納されます。separator パラメータが null 参照 (Visual Basic では Nothing) の場合または文字が含まれていなかった場合、区切り記号に空白文字が指定されたものとして実行されます。
options パラメータが RemoveEmptyEntries で、このインスタンスの長さがゼロの場合、空の配列が返されます。
options パラメータが Noneで、2 つの区切り記号が隣接している場合、または、区切り記号がこのインスタンスの先頭か末尾に見つかった場合、対応する配列の要素には Empty が格納されます。

Split メソッドによって生成される部分文字列を結果に含めるか除外するかを StringSplitOptions 列挙値を使って指定するコード例を次に示します。
' This example demonstrates the String() methods that use ' the StringSplitOptions enumeration. Imports System Class Sample Public Shared Sub Main() Dim s1 As String = ",ONE,,TWO,,,THREE,," Dim s2 As String = "[stop]" & _ "ONE[stop][stop]" & _ "TWO[stop][stop][stop]" & _ "THREE[stop][stop]" Dim charSeparators() As Char = {","c} Dim stringSeparators() As String = {"[stop]"} Dim result() As String ' ------------------------------------------------------------------------------ ' Split a string delimited by characters. ' ------------------------------------------------------------------------------ Console.WriteLine("1) Split a string delimited by characters:" & vbCrLf) ' Display the original string and delimiter characters. Console.WriteLine("1a )The original string is ""{0}"".", s1) Console.WriteLine("The delimiter character is '{0}'." & vbCrLf, 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:" & vbCrLf) ' Display the original string and delimiter string. Console.WriteLine("2a) The original string is ""{0}"".", s2) Console.WriteLine("The delimiter string is ""{0}""." & vbCrLf, 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) End Sub 'Main ' Display the array of separated strings. Public Shared Sub Show(ByVal entries() As String) Console.WriteLine("The return value contains these {0} elements:", entries.Length) Dim entry As String For Each entry In entries Console.Write("<{0}>", entry) Next entry Console.Write(vbCrLf & vbCrLf) End Sub 'Show End Class 'Sample ' '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]> '
// 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 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Split メソッド (Char[], Int32, StringSplitOptions)
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(False)> _ Public Function Split ( _ separator As Char(), _ count As Integer, _ options As StringSplitOptions _ ) As String()
Dim instance As String Dim separator As Char() Dim count As Integer Dim options As StringSplitOptions Dim returnValue As String() returnValue = instance.Split(separator, count, options)
[ComVisibleAttribute(false)] public string[] Split ( char[] separator, int count, StringSplitOptions options )
[ComVisibleAttribute(false)] public: array<String^>^ Split ( array<wchar_t>^ separator, int count, StringSplitOptions options )
/** @attribute ComVisibleAttribute(false) */ public String[] Split ( char[] separator, int count, StringSplitOptions options )
ComVisibleAttribute(false) public function Split ( separator : char[], count : int, options : StringSplitOptions ) : String[]
- separator
この文字列から部分文字列を取り出すために区切り文字として使用する Unicode 文字の配列、区切り文字が含まれていない空の配列、または null 参照 (Visual Basic では Nothing)。
この文字列を、separator 配列のいずれかまたは複数の要素 (文字) で区切ることによって取り出された部分文字列を格納する配列。詳細については、「解説」を参照してください。


戻り値の配列要素に区切り記号文字は含まれません。区切り記号は、序数に基づく比較を使用して検出されます。
このインスタンスに、separator に指定されたいずれの文字も含まれていなかった場合、戻り値の配列には、そのインスタンス自体を保持する要素が 1 つだけ格納されます。separator パラメータが null 参照 (Visual Basic では Nothing) の場合または文字が含まれていなかった場合、区切り記号に空白文字が指定されたものとして実行されます。
count パラメータがゼロの場合、または、options パラメータが RemoveEmptyEntries で、このインスタンスの長さがゼロの場合、空の配列が返されます。
options パラメータが Noneで、2 つの区切り記号が隣接している場合、または、区切り記号がこのインスタンスの先頭か末尾に見つかった場合、対応する配列の要素には Empty が格納されます。
このインスタンスに count を超える部分文字列が含まれていた場合、count - 1 番目にある最初の部分文字列が、戻り値の count - 1 番目の最初の要素として返されます。このインスタンスに含まれるそれ以外の文字は、戻り値の最後の要素として返されます。

Split メソッドによって生成される部分文字列を結果に含めるか除外するかを StringSplitOptions 列挙値を使って指定するコード例を次に示します。
' This example demonstrates the String() methods that use ' the StringSplitOptions enumeration. Imports System Class Sample Public Shared Sub Main() Dim s1 As String = ",ONE,,TWO,,,THREE,," Dim s2 As String = "[stop]" & _ "ONE[stop][stop]" & _ "TWO[stop][stop][stop]" & _ "THREE[stop][stop]" Dim charSeparators() As Char = {","c} Dim stringSeparators() As String = {"[stop]"} Dim result() As String ' ------------------------------------------------------------------------------ ' Split a string delimited by characters. ' ------------------------------------------------------------------------------ Console.WriteLine("1) Split a string delimited by characters:" & vbCrLf) ' Display the original string and delimiter characters. Console.WriteLine("1a )The original string is ""{0}"".", s1) Console.WriteLine("The delimiter character is '{0}'." & vbCrLf, 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:" & vbCrLf) ' Display the original string and delimiter string. Console.WriteLine("2a) The original string is ""{0}"".", s2) Console.WriteLine("The delimiter string is ""{0}""." & vbCrLf, 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) End Sub 'Main ' Display the array of separated strings. Public Shared Sub Show(ByVal entries() As String) Console.WriteLine("The return value contains these {0} elements:", entries.Length) Dim entry As String For Each entry In entries Console.Write("<{0}>", entry) Next entry Console.Write(vbCrLf & vbCrLf) End Sub 'Show End Class 'Sample ' '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]> '
// 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 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Split メソッド

名前 | 説明 |
---|---|
String.Split (Char[]) | このインスタンス内の、指定された Char 配列の要素で区切られた部分文字列を格納する String 配列を返します。 .NET Compact Framework によってサポートされています。 |
String.Split (Char[], Int32) | このインスタンス内の、指定された Char 配列の要素で区切られた部分文字列を格納する String 配列を返します。パラメータには、取得する部分文字列の最大数を指定します。 .NET Compact Framework によってサポートされています。 |
String.Split (Char[], StringSplitOptions) | この文字列内の、指定された Char 配列の要素で区切られた部分文字列を格納する String 配列を返します。空の配列要素を返すかどうかをパラメータで指定します。 .NET Compact Framework によってサポートされています。 |
String.Split (String[], StringSplitOptions) | この文字列内の、指定された String 配列の要素で区切られた部分文字列を格納する String 配列を返します。空の配列要素を返すかどうかをパラメータで指定します。 .NET Compact Framework によってサポートされています。 |
String.Split (Char[], Int32, StringSplitOptions) | この文字列内の、指定された Char 配列の要素で区切られた部分文字列を格納する String 配列を返します。返される部分文字列の最大数と、空の配列要素を返すかどうかをパラメータで指定します。 .NET Compact Framework によってサポートされています。 |
String.Split (String[], Int32, StringSplitOptions) | この文字列内の、指定された String 配列の要素で区切られた部分文字列を格納する String 配列を返します。返される部分文字列の最大数と、空の配列要素を返すかどうかをパラメータで指定します。 .NET Compact Framework によってサポートされています。 |

String.Split メソッド (String[], StringSplitOptions)
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(False)> _ Public Function Split ( _ separator As String(), _ options As StringSplitOptions _ ) As String()
Dim instance As String Dim separator As String() Dim options As StringSplitOptions Dim returnValue As String() returnValue = instance.Split(separator, options)
[ComVisibleAttribute(false)] public string[] Split ( string[] separator, StringSplitOptions options )
[ComVisibleAttribute(false)] public: array<String^>^ Split ( array<String^>^ separator, StringSplitOptions options )
/** @attribute ComVisibleAttribute(false) */ public String[] Split ( String[] separator, StringSplitOptions options )
ComVisibleAttribute(false) public function Split ( separator : String[], options : StringSplitOptions ) : String[]
- separator
この文字列から部分文字列を取り出すために区切り文字として使用する文字列の配列、区切り文字が含まれていない空の配列、または null 参照 (Visual Basic では Nothing)。
この文字列を、separator 配列のいずれかまたは複数の要素 (文字列) で区切ることによって取り出された部分文字列を格納する配列。詳細については、「解説」を参照してください。


戻り値の配列要素に区切り記号文字列は含まれません。区切り記号は、序数に基づく比較を使用して検出されます。
このインスタンスに、separator に指定されたいずれの文字列も含まれていなかった場合、戻り値の配列には、そのインスタンス自体を保持する要素が 1 つだけ格納されます。separator パラメータが null 参照 (Visual Basic では Nothing) の場合または文字が含まれていなかった場合、区切り記号に空白文字が指定されたものとして実行されます。
options パラメータが RemoveEmptyEntries で、このインスタンスの長さがゼロの場合、空の配列が返されます。
options パラメータが Noneで、2 つの区切り記号が隣接している場合、または、区切り記号がこのインスタンスの先頭か末尾に見つかった場合、対応する配列の要素には Empty が格納されます。
Split メソッドは、この文字列を、separator パラメータで指定された 1 つまたは複数の文字列で区切ることによって部分文字列を抽出し、これらの部分文字列を配列の要素として返します。
Split メソッドが区切り記号を検索する際の比較処理では、大文字と小文字は区別され、序数の並べ替え規則が使用されます。単語、文字列、序数の並べ替えの詳細については、System.Globalization.CompareOptions 列挙値のトピックを参照してください。
Split メソッドでは、値が null 参照 (Visual Basic では Nothing) または空の文字列 ("") である separator の要素は無視されます。
separator に格納された複数の文字列に共通の文字が使用されていた場合にあいまいな結果が生じるのを防ぐため、Split の処理では、対象のインスタンスの値を先頭から最後まで解析し、separator 内の要素と最初に一致した要素を区切り文字として使用します。インスタンスにおける部分文字列の出現順が、separator における要素の格納順よりも優先されます。
たとえば、値が "abcdef" であるインスタンスがあるとします。separator の 1 つ目の要素が "ef"、2 つ目の要素が "bcde" であった場合、分割処理の結果は "a" と "f" になります。インスタンス内の部分文字列 "bcde" が、部分文字列 "f" よりも先に separator 内の要素と一致するためです。
ただし、separator の 1 つ目の要素が "bcd" で、2 つ目の要素が "bc" であった場合、分割処理の結果は "a" と "ef" になります。これは、"bcd" が、separator に格納された要素のうち、インスタンス内の区切り記号と一致する最初の要素であるためです。separator に格納された要素の順序が逆だった場合、つまり、1 つ目の要素が "bc" で、2 つ目の要素が "bcd" であった場合、分割処理の結果は "a" と "def" となります。

Split メソッドによって生成される部分文字列を結果に含めるか除外するかを StringSplitOptions 列挙値を使って指定するコード例を次に示します。
' This example demonstrates the String() methods that use ' the StringSplitOptions enumeration. Imports System Class Sample Public Shared Sub Main() Dim s1 As String = ",ONE,,TWO,,,THREE,," Dim s2 As String = "[stop]" & _ "ONE[stop][stop]" & _ "TWO[stop][stop][stop]" & _ "THREE[stop][stop]" Dim charSeparators() As Char = {","c} Dim stringSeparators() As String = {"[stop]"} Dim result() As String ' ------------------------------------------------------------------------------ ' Split a string delimited by characters. ' ------------------------------------------------------------------------------ Console.WriteLine("1) Split a string delimited by characters:" & vbCrLf) ' Display the original string and delimiter characters. Console.WriteLine("1a )The original string is ""{0}"".", s1) Console.WriteLine("The delimiter character is '{0}'." & vbCrLf, 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:" & vbCrLf) ' Display the original string and delimiter string. Console.WriteLine("2a) The original string is ""{0}"".", s2) Console.WriteLine("The delimiter string is ""{0}""." & vbCrLf, 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) End Sub 'Main ' Display the array of separated strings. Public Shared Sub Show(ByVal entries() As String) Console.WriteLine("The return value contains these {0} elements:", entries.Length) Dim entry As String For Each entry In entries Console.Write("<{0}>", entry) Next entry Console.Write(vbCrLf & vbCrLf) End Sub 'Show End Class 'Sample ' '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]> '
// 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 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からString.Splitを検索する場合は、下記のリンクをクリックしてください。

- String.Splitのページへのリンク