TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Regex.Split Examples

Use the Regex.Split method to separate strings based on patterns.
Regex.Split separates a String based on a pattern. The String type's Split Function is adequate for many purposes. But the Regex.Split Function provides character classes and is more robust. With it we develop advanced splitting methods.Regex.MatchSplit
First, as we begin, please notice that the Imports System.Text.RegularExpressions directive is used at the top of this program. This includes the Regex type into the current program.

And: We call the Regex.Split function and pass the pattern \D+ to it. This means one or more non-digit characters.

VB.NET program that uses Regex.Split function Imports System.Text.RegularExpressions Module Module1 Sub Main() ' The input string. Dim sentence As String = "10 cats, 20 dogs, 40 fish and 1 programmer." ' Invoke the Regex.Split shared function. Dim digits() As String = Regex.Split(sentence, "\D+") ' Loop over the elements in the resulting array. For Each item As String In digits Console.WriteLine(item) Next End Sub End Module Output 10 20 40 1
Whitespace. Another use for the Regex.Split function is to break up an input string based on whitespace. Sometimes, an input string may have more than one whitespace character in a row. We demonstrate this in an example.

Tip: Treating more than one whitespace as a single whitespace is useful. The pattern is \s+ and it indicates one or more whitespace characters.

VB.NET program that uses Regex.Split function, removes whitespace Imports System.Text.RegularExpressions Module Module1 Sub Main() ' The input string. Dim expression As String = "3 * 5 = 15" ' Call Regex.Split. Dim operands() As String = Regex.Split(expression, "\s+") ' Loop over the elements. For Each operand As String In operands Console.WriteLine(operand) Next End Sub End Module Output 3 * 5 = 15
Summary. We examined the Regex.Split function, a useful and fairly simple shared function. With it, you can split strings based on patterns more complex than is possible with the String type's Split function.

Note: More examples of Split are available. For simpler requirements, we prefer the String type's implementation.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


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