How to VB.NET String.Split()

The Split() method in VB.Net is a powerful tool that allows you to extract substrings from a given string based on a specified separator. It works by identifying the occurrences of the separator within the original string and splitting it into multiple substrings. These extracted substrings are then returned as separate elements within an array.

When using the Split() method, you provide a separator parameter that defines the character or characters that act as the delimiter for splitting the string. The method scans the original string and identifies instances where the separator appears, using those occurrences as markers to divide the string into smaller segments.

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:
  1. separator - the given delimiter.
Returns:
  1. 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

By calling the Split() method on the sentence string and passing the separator as an argument, the method splits the sentence into individual words based on the spaces. The resulting words are stored in an array called "words".

We can then use a loop to iterate over the "words" array and print each word on a separate line. This allows us to access and manipulate the extracted substrings individually.

VB.Net String Split Example

VB.Net String Split by multiple characters delimiter

In addition to splitting a string using a single character delimiter, the String.Split() method in VB.Net also allows us to split a string based on multiple character delimiters. This enables us to extract substrings from the original string by considering a combination of characters as separators.

To split a string using multiple character delimiters, we can provide an array of characters as the separator parameter in the Split() method. The method will then identify any occurrence of these characters in the string and split it accordingly.

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

To split a string by multiple characters delimiter using regular expressions in VB.Net, you can utilize the Regex.Split() method from the System.Text.RegularExpressions namespace. This method allows you to split a string based on a regular expression pattern rather than a specific character or substring.

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

You can remove all whitespace from a string using the StringSplitOptions.RemoveEmptyEntries option. This option ensures that the return value of the split operation does not include any array elements that contain an empty string.

Full Source VB.NET
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

Conclusion

By using the String.Split() method with multiple character delimiters, we can easily break down a string into meaningful substrings based on various combinations of characters. This offers flexibility in handling complex string patterns and enhances the versatility of our string manipulation operations.