VB.NET Split String

In this article you will learn how to split string in VB.NET.
  • 6185

String class provides the Split method that is used to split a string delimited with some specified characters. It identifies the substrings that are delimited by one or more characters specified in an array, and then return these substrings in a string array. Delimiter characters are not included in the substrings.

The Split function returns one of these:

  • An array consisting of a single element containing the entire string if the string contains none of the characters in the separator list.

  • An array of substrings if the string is delimited by one or more of the characters specified in the separator list passed in the Split method.

  • An array of substrings in a string delimited by white space characters if those characters occur and the separator array passed is a null reference or contains no delimiter characters.

For example, if we want to split string 38,\n29, 57 with a character array separator list containing comma and space characters, we will get "38", "", "29", "", "57" as string array elements returned. Or, if we want to split string "38..29..57" with the character array delimiter list containing a period '.' , we will get "38", "", "29", "", "57" as string array elements. 

Here is a complete example of using various separators to split different strings. 

    Module Module1
        Sub Main()
            ' Some strings with separated by space, comma, and a substring
            Dim intSpacedString As String = "1 3 5 7 9"
            Dim charCommaString As String = " a, e, i, o, u"
            Dim nameCommaString As String = "Mahesh,Raj,,,Dinesh,Mike,Praveen,Joe,Lana"
            Dim subStringString As String = "12KKK34KKK56KKK78KKK"
            Dim spaceSeparator As Char() = New Char() {" "c}
            Dim commaSeparator As Char() = New Char() {","c}
            Dim stringSeparators As String() = New String() {"KKK"}
            Dim result As String()
            Console.WriteLine("=======================================")
            Console.WriteLine("Space separated strings :" & vbLf)
            result = intSpacedString.Split(spaceSeparator,
StringSplitOptions.None)

            For Each str As String In result
                Console.WriteLine(str)
            Next
            Console.WriteLine("=======================================")
            Console.WriteLine("Comma separated strings :" & vbLf)
            result = charCommaString.Split(commaSeparator,
StringSplitOptions.None)

            For Each str As String In result
                Console.WriteLine(str)
            Next
            result = nameCommaString.Split(commaSeparator, StringSplitOptions.None)

            For Each str As String In result
                Console.WriteLine(str)
            Next
            Console.WriteLine("=======================================")
            Console.WriteLine("Substring separated strings :" & vbLf)
            result = subStringString.Split(stringSeparators,
StringSplitOptions.None)

            For Each str As String In result
                Console.WriteLine(str)
            Next
            Console.WriteLine("=======================================")
            Console.ReadKey()
        End Sub
    End
Module
 

Output

split1.gif
 

Conclusion

Hope this article would have helped you in understanding Split String in VB.NET.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.