VB.NET Split String Examples

VB.NET Split String Examples This VB.NET tutorial provides examples for the String.Split function. It uses Char and String delimiters.

Split. This function separates Strings.

It receives a string or character delimiter. It parses and separates the source string by separating the parts that come between the delimiter.

An example. To start, we split on a space character. We allocate a New Char array as well as a String array to store the words. Finally we loop over the Strings and display them.

Char array: The "New Char" syntax is used to create the char array. This is a one-element char array, with one space char.

Char Array

Based on: .NET 4.5 VB.NET program that uses Split Module Module1 Sub Main() ' We want to split this input string. Dim s As String = "there is a cat" ' Split string based on spaces. Dim words As String() = s.Split(New Char() {" "c}) ' Use For Each loop over words and display them. Dim word As String For Each word In words Console.WriteLine(word) Next End Sub End Module Output there is a cat

File path parts. Here we split a file system path into separate parts. We use a New Char array with one string, "\""c. We then loop through and display the results.

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

Regex.Split words. Often we need to extract words from a String. The code here needs to handle punctuation and non-word characters differently than the String Split method.

Regex.Split

Pattern: The pattern "\W+" is used, and this means "one or more non-word characters". This pattern will match punctuation and spaces.

Note: Regex Functions tend to be slower. They can handle much more complex patterns than the String Split Function.

And: It is best to use the fastest and simplest Function that handles your problem.

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

File lines. Next, we see one way to Split each line in a file using File.ReadAllLines and Split. We have a comma-separated-values CSV file. It first reads in the file with ReadAllLines.

Then: This function puts each line in the file into an array element. The example Splits on ","c. We show the output of the program.

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

RemoveEmptyEntries. Sometimes there are no characters between two delimiters. This results in an empty string in the result array. We can remove this with a "StringSplitOptions" argument.

Here: With StringSplitOptions.RemoveEmptyEntries we change the behavior of split to avoid empty strings in the result array.

Tip: Two empty elements are removed. If we use the Split method with RemoveEmptyEntries, two additional strings are returned in the array.

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

A summary. We saw some ways to Split a String in VB.NET. Split returns a String array that you can use in a For Each loop. We used Split on different characters and strings.

Regex. We called Regex.Split in a more complex pattern example. With Regex, we can split based on patterns and metacharacters. This is more powerful but also more complex.

.Net

.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Sort Split Static StringBuilder Substring Switch Tuple

Java

Core Array ArrayList HashMap String 2D Cast Character Console Deque Duplicates File For Format HashSet If IndexOf Lambda Math ParseInt Process Random Regex Replace Sort Split StringBuilder Substring Switch Vector While

Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf

Từ khóa » Visual Basic String Array Split