Export (0) Print
Expand All

Strings.Split Method

Returns a zero-based, one-dimensional array containing a specified number of substrings.

Namespace: Microsoft.VisualBasic
Assembly: Microsoft.VisualBasic (in microsoft.visualbasic.dll)

public static string[] Split (
	string Expression,
	[OptionalAttribute] string Delimiter,
	[OptionalAttribute] int Limit,
	[OptionalAttribute] CompareMethod Compare
)
public static String[] Split (
	String Expression, 
	/** @attribute OptionalAttribute() */ String Delimiter, 
	/** @attribute OptionalAttribute() */ int Limit, 
	/** @attribute OptionalAttribute() */ CompareMethod Compare
)
public static function Split (
	Expression : String, 
	Delimiter : String, 
	Limit : int, 
	Compare : CompareMethod
) : String[]

Parameters

Expression

Required. String expression containing substrings and delimiters.

Delimiter

Optional. Any single character used to identify substring limits. If Delimiter is omitted, the space character (" ") is assumed to be the delimiter.

Limit

Optional. Maximum number of substrings into which the input string should be split. The default, –1, indicates that the input string should be split at every occurrence of the Delimiter string.

Compare

Optional. Numeric value indicating the comparison to use when evaluating substrings. See "Settings" for values.

Return Value

String array. If Expression is a zero-length string (""), Split returns a single-element array containing a zero-length string. If Delimiter is a zero-length string, or if it does not appear anywhere in Expression, Split returns a single-element array containing the entire Expression string.

For more detailed information, see the Visual Basic topic Split Function (Visual Basic).

By default, or when Limit equals -1, the Split function splits the input string at every occurrence of the delimiter string, and returns the substrings in an array. When the Limit parameter is greater than zero, the Split function splits the string at the first Limit-1 occurrences of the delimiter, and returns an array with the resulting substrings. For example, Split("a:b:c", ":") returns the array {"a", "b", "c"}, while Split("a:b:c", ":", 2) returns the array {"a", "b:c"}.

When the Split function encounters two delimiters in a row, or a delimiter at the beginning or end of the string, it interprets them as surrounding an empty string (""). For example, Split("xx", "x") returns the array containing three empty strings: one from between the beginning of the string and the first "x", one from between the two "x" strings, and one from between the last "x" and the end of the string.

This table demonstrates how the optional Delimiter, Limit, and Compare parameters can change the behavior of the Split function.

Split Call

Return Value

Split("42, 12, 19")

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

Split("42, 12, 19", ", ")

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

Split("42, 12, 19", ", ", 2)

{"42", "12, 19"}

Split("192.168.0.1", ".")

{"192", "168", "0", "1"}

Split("Alice and Bob", " AND ")

{"Alice and Bob"}

Split("Alice and Bob", " AND ", ,CompareMethod.Text)

{"Alice", "Bob"}

Split("someone@example.com", "@",1)

{"someone@example.com"}

Split("someone@example.com", "@",2)

{"someone", "example.com"}

The Compare argument can have the following values.

Constant

Description

Value

CompareMethod.Binary

Performs a binary comparison

0

CompareMethod.Text

Performs a textual comparison

1

The following example demonstrates how to split a string at its spaces.

Dim TestString As String = "Look at these!"
' Returns an array containing "Look", "at", and "these!".
Dim TestArray() As String = Split(TestString)

The following example demonstrates how to split strings with multiple delimiters in a row and filter out the empty strings.

Dim TestString As String = "apple    pear banana  "
Dim TestArray() As String = Split(TestString)
' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
Dim LastNonEmpty As Integer = -1
For i As Integer = 0 To TestArray.Length - 1
    If TestArray(i) <> "" Then
        LastNonEmpty += 1
        TestArray(LastNonEmpty) = TestArray(i)
    End If
Next
ReDim Preserve TestArray(LastNonEmpty)
' TestArray now holds {"apple", "pear", "banana"}

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft