ARTICLE

Split String in VB.NET

Posted by Mahesh Chand Articles | Strings, Arrays in VB.NET August 25, 2010
In this article you will learn how to split string in VB.NET.
 
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

sir, how can i split pipe delimited string?

Posted by keith aranjuez Jan 27, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor