Split
This VB.NET function separates Strings. It receives a string
or character delimiter. It parses and separates the source string
by getting the parts that come between the delimiter.
After calling Split
, we receive an array of String
elements. With RemoveEmptyEntries
(an enum
) we can eliminate empty strings from the result of Split
.
The Split()
Function is used in many programs. The simplest call receives a Char
array, and returns a String
array.
break
apart into 4 separate word strings. Notice how it contains 3 spaces.Split
. The "New Char
" syntax is used to create the char
array. This is a one-element char
array, with one space char
.For Each
loop over the result "words" array and print each one to the screen.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 ModuleWORD: there WORD: is WORD: a WORD: cat
Here we split a file system path into separate parts. We use a New Char
array with one string
containing a backslash. We then loop through and display the results.
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. For Each part As String In parts Console.WriteLine(part) Next End Sub End ModuleC: Users Sam Documents Perls Main
Regex.Split
wordsOften we need to extract words from a String
. The code here needs to handle punctuation and non-word characters differently than the String
Split
function.
Regex.Split
is the source string
we are trying to separate apart.Regex
pattern. The pattern "\W+" is used, and this means "one or more non-word characters."Regex
functions tend to be slower. But they can handle more complex patterns than the String
Split
function.Imports System.Text.RegularExpressions Module Module1 Sub Main() ' 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 As String In arr Console.WriteLine(s) Next 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 ModuleThat is a cute cat man
Here we Split
each line in a file using the File.ReadAllLines
Function and Split
. We have a comma-separated-values (CSV) file. We first read in the file with ReadAllLines
.
File.ReadAllLines
Function puts each line in the file into an array, which we can loop over.Split()
with a Char
array with 1 element—the comma char
. This separates each line on the comma.Imports System.IO Module Module1 Sub Main() Dim i As Integer = 0 ' Loop through each line in array returned by ReadAllLines. For Each line As String In File.ReadAllLines("example.txt") ' Split line on comma. Dim parts As String() = line.Split(New Char() {","c}) ' Loop over each string received. For Each part As String In parts ' Display to console. Console.WriteLine("{0}:{1}", i, part) Next i += 1 Next End Sub End Modulefrontal,parietal,occipital,temporal pulmonary artery,aorta,left ventricle0:frontal 0:parietal 0:occipital 0:temporal 1:pulmonary artery 1:aorta 1:left ventricle
RemoveEmptyEntries
Sometimes there are no characters between two delimiters. This results in an empty string
in the result array.
char
.RemoveEmptyEntries
as the second argument. This avoids empty strings in the result array.Split()
without RemoveEmptyEntries
, 2 additional strings are returned.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 Modulecat dog fish
char
splitSuppose we need to split on a 1-character string
. We could use a 1-char String
in a String
array, or a Char
in a Char
array. The performance may be affected.
String
. The result here has 3 Strings—the string
is split on a comma.Split
directly on a Char
. In VB.NET we specify a Char
in the same way as a String
but use the suffix "c."Char
array argument, is slightly faster—we should use a Char
array when possible.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 Module117.60 ns Split(String()) 97.15 ns Split(Char())
We examined ways to invoke the Split()
Function. Split
returns a String
array that we can use in a For Each
loop. We used Split
on different characters and strings.