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 that will contain substrings that are delimited by the specified characters in an array.

 

Following is the pictorial representation of Split method functionality in a 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 the Split method. After completion of splitting the string, the split method will return a string array like as shown in the above diagram.

Visual Basic String Split Method Syntax

Following is the syntax of defining a Split method in a 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 delimiters to split the given string into substrings and return it as a string array.

Visual Basic String Split Method Example

Following is the example of splitting the given string with comma (“,”) delimiter using Split() method in a 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 a comma (',') delimiter and returning substrings as a 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 as shown below.

 

Visual Basic String Split Method Example Result

 

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

Visual Basic Split String with Multiple Delimiters

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

 

Following is the example of splitting the given string with multiple delimiters in a 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 a Split() method with multiple delimiters to split the given string into a 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 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 a Split() method in a 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 a 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 the Split method string array result as a list using List object. We will learn more about the List in the next chapters.

 

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

 

Visual Basic Split String into List Example Result

 

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