C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Part 1: This is the input string we are trying to break apart into 4 separate word strings. Notice how it contains 3 spaces.
Part 2: We call Split. The "New Char" syntax is used to create the char array. This is a one-element char array, with one space char.
Char ArrayPart 3: We use the For Each loop over the result "words" array and print each one to the screen.
VB.NET program that uses Split
Module Module1
Sub Main()
' Part 1: we want to split this input string.
Dim s As String = "there is a cat"
' Part 2: split string based on spaces.
Dim words As String() = s.Split(New Char() {" "c})
' Part 3: use For Each loop over words.
' ... Display each word on the screen.
For Each word As String In words
Console.WriteLine("WORD: {0}", word)
Next
End Sub
End Module
Output
WORD: there
WORD: is
WORD: a
WORD: cat
VB.NET program that splits file path
Module Module1
Sub Main()
' The file system path we need to split.
Dim s As String = "C:\Users\Sam\Documents\Perls\Main"
' Split the string on the backslash character.
Dim parts As String() = s.Split(New Char() {"\"c})
' Loop through result strings with For Each.
Dim part As String
For Each part In parts
Console.WriteLine(part)
Next
End Sub
End Module
Output
C:
Users
Sam
Documents
Perls
Main
Argument 1: The first argument to Regex.Split is the source string we are trying to separate apart.
Argument 2: The second argument is a Regex pattern. The pattern "\W+" is used, and this means "one or more non-word characters".
Warning: Regex functions tend to be slower. But they can handle more complex patterns than the String Split function.
VB.NET program that splits words
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' Declare iteration variable.
Dim s As String
' Loop through words in string.
Dim arr As String() = SplitWords("That is a cute cat, man!")
' Display each word.
' ... Note that punctuation is handled correctly.
For Each s In arr
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
''' <summary>
''' Split the words in string on non-word characters.
''' This means commas and periods are handled correctly.
''' </summary>
Private Function SplitWords(ByVal s As String) As String()
' Call Regex.Split function from the imported namespace.
' ... Return the result array.
Return Regex.Split(s, "\W+")
End Function
End Module
Output
That
is
a
cute
cat
man
Info: The File.ReadAllLines Function puts each line in the file into an array, which we can loop over.
FileSplit: We call Split() with a Char array with 1 element—the comma char. This separates each line on the comma.
Input file used
frontal,parietal,occipital,temporal
pulmonary artery,aorta,left ventricle
VB.NET program that splits lines
Imports System.IO
Module Module1
Sub Main()
Dim i As Integer = 0
' Loop through each line in array returned by ReadAllLines.
Dim line As String
For Each line In File.ReadAllLines("example.txt")
' Split line on comma.
Dim parts As String() = line.Split(New Char() {","c})
' Loop over each string received.
Dim part As String
For Each part In parts
' Display to console.
Console.WriteLine("{0}:{1}", i, part)
Next
i += 1
Next
End Sub
End Module
Output
0:frontal
0:parietal
0:occipital
0:temporal
1:pulmonary artery
1:aorta
1:left ventricle
Argument 1: We pass a character array as the first argument. We specify we want to split on a comma char.
Argument 2: We pass RemoveEmptyEntries as the second argument. This avoids empty strings in the result array.
Result: Two empty elements are removed. If we use Split() without RemoveEmptyEntries, 2 additional strings are returned.
VB.NET program that uses RemoveEmptyEntries
Module Module1
Sub Main()
Dim value As String = "cat,dog,,,fish"
' Split string on comma characters.
' ... Remove empty elements from result.
Dim elements() As String =
value.Split(New Char() {","c},
StringSplitOptions.RemoveEmptyEntries)
' Display elements.
For Each element As String In elements
Console.WriteLine(element)
Next
End Sub
End Module
Output
cat
dog
fish
Version 1: We split on a 1-char String. The result here has 3 Strings—the string is split on a comma.
Version 2: We call Split directly on a Char. In VB.NET we specify a Char in the same way as a String but use the suffix "c."
Result: Version 2, which uses a Char array argument, is slightly faster—we should use a Char array when possible.
VB.NET program that times split, char array argument
Module Module1
Sub Main()
Dim m As Integer = 10000000
' Version 1: split on string array.
Dim s1 As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To m - 1
Dim result() As String = "cat,frog,dog".Split(New String() {","}, StringSplitOptions.None)
If result.Length <> 3 Then
Return
End If
Next
s1.Stop()
' Version 2: split on char array.
Dim s2 As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To m - 1
Dim result() As String = "cat,frog,dog".Split(New Char() {","c})
If result.Length <> 3 Then
Return
End If
Next
s2.Stop()
Dim u As Integer = 1000000
Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns"))
Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns"))
End Sub
End Module
Output
117.60 ns Split(String())
97.15 ns Split(Char())