次の方法で共有


String.Split メソッド (Char )

このインスタンス内で、配列内で指定された 1 つ以上の文字で区切られた部分文字列を識別し、次にこの部分文字列を String 配列に格納します。

Overloads Public Function Split( _
   ByVal ParamArray separator() As Char _) As String()
[C#]
public string[] Split(   params char[] separator);
[C++]
public: String* Split(__wchar_tseparator __gc[])  __gc[];
[JScript]
public function Split(
   separator : Char[]) : String[];

パラメータ

  • separator
    このインスタンスでの部分文字列の区切り文字である Unicode 文字の配列、区切り文字が含まれていない空の配列、または null 参照 (Visual Basic では Nothing) 。

戻り値

separator の文字がこのインスタンスに含まれていない場合には、このインスタンスが含まれている単一要素で構成される配列。

または

このインスタンスが separator の文字で区切られている場合は、部分文字列の配列。

または

これらの文字が出現し、 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

[C#] 
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);
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;

int main()
{
   String* words = S"this is a list of words, with: a bit of punctuation.";
   Char chars[] = {' ', ', ', '->', ':'};
   String* split[] = words->Split(chars);

   IEnumerator* myEnum = split->GetEnumerator();
   while (myEnum->MoveNext()) {
      String* s = __try_cast<String*>(myEnum->Current);
      if (!s->Trim()->Equals(S""))
         Console::WriteLine(s);
   }
}

[JScript] 
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 NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | String.Split オーバーロードの一覧 | Char | Concat | Insert | Join | Remove | Replace | Substring | Trim