Net-informations.com
SiteMap  | About    

How to VB.NET String.Split()

The VB.Net Split() extracts the substrings from the given string that are delimited by the separator parameter, and returns those substrings as elements of an array.

split-string-vb

If your String contains "dd-mm-yy", split on the "-" character to get an array of: "dd" "mm" "yy".

If the separator parameter is null or contains no characters, white space characters are assumed to be the delimiters.

Syntax :

Public Function Split(ByVal ParamArray separator() As Char) As String()

Parameters:

separator - the given delimiter

Returns:

An array of Strings delimited by one or more characters in separator

Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim str As String Dim strArr() As String Dim count As Integer str = "vb.net split test" strArr = str.Split(" ") For count = 0 To strArr.Length - 1 MsgBox(strArr(count)) Next End Sub End Class

Output:

vb.net split test

VB.Net String Split Example

VB.Net String Split by multiple characters delimiter

We can split a string by multiple character delimiter using String.split() method.

Dim input As String = "one)(two)(three)(four)(five" Dim result As String() = input.Split(New String() {")("}, StringSplitOptions.None) For Each s As String In result MessageBox.Show(s) Next

Output:

one two three four five

Using Regular Expressions for multiple characters

VB.Net String Split by multiple characters delimiter using Regular Expressions

You should import System.Text.RegularExpressions in your project to run the following code

Dim input As String = "one)(two)(three)(four)(five" Dim result As String() = Regex.Split(input, "\)\(") For Each s As String In result MessageBox.Show(s) Next

Output:

one two three four five

Split() a delimited string to a List < String > in VB.Net

You can retrieve the result of a String splt() method to a VB.Net List. The following program convert the String Array to a List.

VB.Net Convert List to String

Dim s As String = "This is a sentence." Dim list As IList(Of String) = New List(Of String)(s.Split(New String() {" "}, StringSplitOptions.None)) For Each element As String In list MessageBox.Show(element) Next

VB.NetString split White spaces

StringSplitOptions.RemoveEmptyEntries guarantees the return value does not include array elements that contain an empty string. The following VB.Net program shows how to remove all white spaces from string using StringSplitOptions.RemoveEmptyEntries.




Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myStrA As String = "one two   three    four     five"
        Dim result As String() = myStrA.Split(New Char(-1) {}, StringSplitOptions.RemoveEmptyEntries)
        For Each s As String In result
            MessageBox.Show(s)
        Next
    End Sub
End Class

When you execute this VB.Net program you will get the result removing all white spaces from the string

 VB.NET String - Related Contents


 VB.NET String - Related Programs