String.Split Method (Char[])
Identifies the substrings in this instance that are delimited by one or more characters specified in an array, then places the substrings into a String array.
[Visual Basic] Overloads Public Function Split( _ ByVal ParamArray separator() As Char _ ) As String() [C#] public string[] Split( params char[] separator ); [C++] public: String* Split( __wchar_t separator __gc[] ) __gc[]; [JScript] public function Split( separator : Char[] ) : String[];
Parameters
- separator
- An array of Unicode characters that delimit the substrings in this instance, an empty array containing no delimiters, or a null reference (Nothing in Visual Basic).
Return Value
An array consisting of a single element containing this instance, if this instance contains none of the characters in separator.
-or-
An array of substrings if this instance is delimited by one or more of the characters in separator.
-or-
An array of the substrings in this instance delimited by white space characters if those characters occur and separator is a null reference (Nothing in Visual Basic) or contains no delimiter characters.
Empty is returned for any substring where two delimiters are adjacent, or a delimiter is found at the beginning or end of this instance.
Delimiter characters are not included in the substrings.
Remarks
For example:
| Input | separator | Output |
|---|---|---|
| "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"} |
Example
The following code example demonstrates how you can tokenize a string with the Split method.
[Visual Basic] 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();
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
String Class | String Members | System Namespace | String.Split Overload List | Char | Concat | Insert | Join | Remove | Replace | Substring | Trim