String.Split メソッドとは? わかりやすく解説

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

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

この文字列内の指定されString 配列要素区切られ部分文字列格納する String 配列返します返される部分文字列最大数と、空の配列要素返すかどうかパラメータ指定します

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

<ComVisibleAttribute(False)> _
Public Function Split ( _
    separator As String(), _
    count As Integer, _
    options As StringSplitOptions _
) As String()
[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)。

count

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

options

返される配列で空の要素省略する場合は RemoveEmptyEntries。返される配列に空の要素含め場合は None。

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

例外例外
例外種類条件

ArgumentOutOfRangeException

count が負の値です。

ArgumentException

options が StringSplitOptions 値ではありません。

解説解説
戻り値に関する情報

戻り値配列要素区切り記号文字列含まれません。区切り記号は、序数に基づく比較使用して検出されます。

このインスタンスに、separator指定されいずれの文字列含まれていなかった場合戻り値配列には、そのインスタンス自体保持する要素1 つだけ格納されます。separator パラメータnull 参照 (Visual Basic では Nothing) の場合または文字含まれていなかった場合区切り記号空白文字指定されたものとして実行されます。

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

options パラメータNoneで、2 つ区切り記号隣接している場合、または、区切り記号がこのインスタンス先頭末尾見つかった場合対応する配列要素には Empty格納されます。

このインスタンスcount超える部分文字列含まれていた場合count - 1 番目にある最初部分文字列が、戻り値count - 1 番目の最初要素として返されます。このインスタンス含まれるそれ以外文字は、戻り値最後要素として返されます。

count部分文字列の数よりも多い場合取り出せるだけの部分文字列返され例外スローされません。

比較に関する情報

Split メソッドは、この文字列を、separator パラメータ指定され1 つまたは複数文字列区切ることによって部分文字列抽出し、これらの部分文字列配列要素として返します

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

Split メソッドでは、値が null 参照 (Visual Basic では Nothing) または空の文字列 ("") である separator要素無視されます。

separator格納され複数文字列に共通の文字使用されていた場合あいまいな結果生じるのを防ぐため、Split メソッドは、対象インスタンスの値を先頭から最後まで解析しseparator 内の要素最初に一致した要素区切り文字として使用しますインスタンスにおける部分文字列出現順が、separator における要素格納順よりも優先されます。

たとえば、値が "abcdef" であるインスタンスがあるとしますseparator1 つ目の要素が "ef"、2 つ目の要素が "bcde" であった場合分割処理の結果は "a" と "f" になりますインスタンス内の部分文字列 "bcde" が、部分文字列 "f" よりも先に separator 内の要素一致するためです。

ただし、separator1 つ目の要素が "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]>

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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

このインスタンス内の指定されChar 配列要素区切られ部分文字列格納する String 配列返します

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

解説解説

戻り値配列要素区切り記号文字含まれません。区切り記号は、序数に基づく比較使用して検出されます。

このインスタンスに、separator指定されいずれの文字含まれていなかった場合戻り値配列には、そのインスタンス自体保持する要素1 つだけ格納されます。separator パラメータnull 参照 (Visual Basic では Nothing) の場合または文字含まれていなかった場合区切り記号空白文字指定されたものとして実行されます。

2 つ区切り記号隣接している場合、または、区切り記号がこのインスタンス先頭末尾見つかった場合対応する配列要素には Empty格納されます。

次に例を示します

入力

区切り記号

出力

"42, 12, 19"

new Char[] {',', ' '}

{"42", "", "12", "", "19"}

"42..12..19"

new Char[] {'.'}

{"42", "", "12", "", "19"}

"Banana"

new Char[] {'.'}

{"Banana"}

"Darb\nSmarba"

new Char[] {}

{"Darb", "Smarba"}

"Darb\nSmarba"

null

{"Darb", "Smarba"}

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

使用例使用例

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();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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

このインスタンス内の指定されChar 配列要素区切られ部分文字列格納する String 配列返しますパラメータには、取得する部分文字列最大数を指定します

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

例外例外
例外種類条件

ArgumentOutOfRangeException

count が負の値です。

解説解説

戻り値配列要素区切り記号文字含まれません。区切り記号は、序数に基づく比較使用して検出されます。

このインスタンスに、separator指定されいずれの文字含まれていなかった場合戻り値配列には、そのインスタンス自体保持する要素1 つだけ格納されます。separator パラメータnull 参照 (Visual Basic では Nothing) の場合または文字含まれていなかった場合区切り記号空白文字指定されたものとして実行されます。countゼロ場合、空の配列返されます。

2 つ区切り記号隣接している場合、または、区切り記号がこのインスタンス先頭末尾見つかった場合対応する配列要素には Empty格納されます。

このインスタンスcount超える部分文字列含まれていた場合count - 1 番目にある最初部分文字列が、戻り値count - 1 番目の最初要素として返されます。このインスタンス含まれるそれ以外文字は、戻り値最後要素として返されます。

count部分文字列の数よりも多い場合取り出せるだけの部分文字列返され例外スローされません。

次に例を示します

入力

区切り記号カウント

出力

"42, 12, 19"

new Char[] {',', ' '}, 2

{"42", " 12, 19"}

"42..12..19"

new Char[] {'.'}, 4

{"42", "", "12", ".19"}

"Banana"

new Char[] {'.'}, 2

{"Banana"}

"Darb\nSmarba"

new Char[] {}, 1

{"Darb\nSmarba"}

"Darb\nSmarba"

new Char[] null, 2

{"Darb", "Smarba"}

"Darb\nSmarba"

new Char[] null, 100

{"Darb", "Smarba"}

使用例使用例

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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

この文字列内の指定されChar 配列要素区切られ部分文字列格納する String 配列返します。空の配列要素返すかどうかパラメータ指定します

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

<ComVisibleAttribute(False)> _
Public Function Split ( _
    separator As Char(), _
    options As StringSplitOptions _
) As String()
[ComVisibleAttribute(false)] 
public string[] Split (
    char[] separator,
    StringSplitOptions 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)。

options

返される配列で空の要素省略する場合は RemoveEmptyEntries。返される配列に空の要素含め場合は None。

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

例外例外
例外種類条件

ArgumentException

options が StringSplitOptions 値ではありません。

解説解説
使用例使用例

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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

この文字列内の指定されChar 配列要素区切られ部分文字列格納する String 配列返します返される部分文字列最大数と、空の配列要素返すかどうかパラメータ指定します

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

<ComVisibleAttribute(False)> _
Public Function Split ( _
    separator As Char(), _
    count As Integer, _
    options As StringSplitOptions _
) As String()
[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)。

count

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

options

返される配列で空の要素省略する場合は RemoveEmptyEntries。返される配列に空の要素含め場合は None。

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

例外例外
例外種類条件

ArgumentOutOfRangeException

count が負の値です。

ArgumentException

options が StringSplitOptions 値ではありません。

解説解説

戻り値配列要素区切り記号文字含まれません。区切り記号は、序数に基づく比較使用して検出されます。

このインスタンスに、separator指定されいずれの文字含まれていなかった場合戻り値配列には、そのインスタンス自体保持する要素1 つだけ格納されます。separator パラメータnull 参照 (Visual Basic では Nothing) の場合または文字含まれていなかった場合区切り記号空白文字指定されたものとして実行されます。

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

options パラメータNoneで、2 つ区切り記号隣接している場合、または、区切り記号がこのインスタンス先頭末尾見つかった場合対応する配列要素には Empty格納されます。

このインスタンスcount超える部分文字列含まれていた場合count - 1 番目にある最初部分文字列が、戻り値count - 1 番目の最初要素として返されます。このインスタンス含まれるそれ以外文字は、戻り値最後要素として返されます。

count部分文字列の数よりも多い場合取り出せるだけの部分文字列返され例外スローされません。

使用例使用例

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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Split メソッド

このインスタンス内の指定されChar 配列または String 配列要素区切られ部分文字列格納する String 配列返します
オーバーロードの一覧オーバーロードの一覧

名前 説明
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)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

この文字列内の指定されString 配列要素区切られ部分文字列格納する String 配列返します。空の配列要素返すかどうかパラメータ指定します

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

<ComVisibleAttribute(False)> _
Public Function Split ( _
    separator As String(), _
    options As StringSplitOptions _
) As String()
[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)。

options

返される配列で空の要素省略する場合は RemoveEmptyEntries。返される配列に空の要素含め場合は None。

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

例外例外
例外種類条件

ArgumentException

options が StringSplitOptions 値ではありません。

解説解説
戻り値に関する情報
比較に関する情報

Split メソッドは、この文字列を、separator パラメータ指定され1 つまたは複数文字列区切ることによって部分文字列抽出し、これらの部分文字列配列要素として返します

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

Split メソッドでは、値が null 参照 (Visual Basic では Nothing) または空の文字列 ("") である separator要素無視されます。

separator格納され複数文字列に共通の文字使用されていた場合あいまいな結果生じるのを防ぐため、Split の処理では、対象インスタンスの値を先頭から最後まで解析しseparator 内の要素最初に一致した要素区切り文字として使用しますインスタンスにおける部分文字列出現順が、separator における要素格納順よりも優先されます。

たとえば、値が "abcdef" であるインスタンスがあるとしますseparator1 つ目の要素が "ef"、2 つ目の要素が "bcde" であった場合分割処理の結果は "a" と "f" になりますインスタンス内の部分文字列 "bcde" が、部分文字列 "f" よりも先に separator 内の要素一致するためです。

ただし、separator1 つ目の要素が "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]>

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「String.Split メソッド」の関連用語

String.Split メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



String.Split メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS