Visual Basic String Split Method

In visual basic, the string Split method is useful to split a string into substrings based on the characters in an array. The split method will return a string array which will contain a substrings that are delimited by the specified characters in an array.

 

Following is the pictorial representation of Split method functionality in visual basic programming language.

 

Visual Basic String Split Method Functionality Representation Diagram

 

If you observe the above diagram, we are splitting the string “Suresh-Rohini-Trishika” with delimiter “-” using Split method. After completion of splitting the string, the split method will return a string array like as shown in above diagram.

Visual Basic String Split Method Syntax

Following is the syntax of defining a Split method in visual basic programming language.

 

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

If you observe the above syntax, we are using character array (Char()) to define a delimiters to split the given string into substrings and return it as string array.

Visual Basic String Split Method Example

Following is the example of splitting the given string with comma (“,”) delimiter using Split() method in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim msg As String = "Suresh,Rohini,Trishika"

        Dim strarr As String() = msg.Split(","c)

        For i As Integer = 0 To strarr.Length - 1

            Console.WriteLine(strarr(i))

        Next

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used Split() method to split the given string ("Suresh,Rohini,Trishika") with comma (',') delimiter and returning substrings as string array. Here we used a for loop to iterate through string array to display array elements.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic String Split Method Example Result

 

This is how we can split the string with required delimiters using Split() method in visual basic programming language.

Visual Basic Split String with Multiple Delimiters

In the above example, we used only one comma delimiter with Split() method to split the given string. We can also use multiple delimiters with Split() method to split the given string and we can remove the empty subscripts in string array.

 

Following is the example of splitting the given string with multiple delimiters in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim msg As String = "Suresh,Rohini,Trishika,-Praveen%Sateesh"

        Dim strarr As String() = msg.Split(New Char() {","c, "-"c, "%"c}, StringSplitOptions.RemoveEmptyEntries)

        For i As Integer = 0 To strarr.Length - 1

            Console.WriteLine(strarr(i))

        Next

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used Split() method with multiple delimiters to split the given string into string array.

 

Here the StringSplitOptions.RemoveEmptyEntries property is useful to remove the empty string elements while returning the result array.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Split String with Multiple Delimiters Example Result

 

This is how we can split the given string with multiple delimiters using Split() method in visual basic programming language.

Visual Basic Split String into List

Generally, the Split method in visual basic will return a result as a string array. In case, if we want to return a result as a list, then we can convert the string array to list using List object.

 

Following is the example to return a split method result as a list in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim msg As String = "Suresh,Rohini,Trishika,-Praveen%Sateesh"

        Dim list As IList(Of String) = New List(Of String)(msg.Split(New Char() {","c, "-"c, "%"c}, StringSplitOptions.RemoveEmptyEntries))

        For i As Integer = 0 To list.Count - 1

            Console.WriteLine(list(i))

        Next

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are converting Split method string array result as a list using List object. We will learn more about List in the next chapters.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Split String into List Example Result

 

This is how we can return Split method result as a list based on our requirements in visual basic programming language.