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 Remove Punctuation From String

Remove all punctuation characters from the start and end of strings. Use Char.IsPunctuation.
Remove punctuation. Strings often contain unneeded characters. In VB.NET there are many ways to remove punctuation characters from strings. Here we use the Char.IsPunctuation function. It handles all punctuation characters.
Example. We introduce a function called TrimPunctuation. In this function, we count the number of punctuation characters at the start and the end. We use two for-loops to do this. We call Char.IsPunctuation to check each character.

Substring: We use the Substring method to eliminate all the punctuation characters we counted. We use the counts as offsets.

Finally: We loop over the string values and display the trimmed version. This helps us check correctness of the method.

VB.NET program that removes punctuation Module Module1 Sub Main() Dim values() As String = {"One?", "--two--", "...three!", "four", "", "five*"} ' Loop over strings and call TrimPunctuation. For Each value As String In values Console.WriteLine(TrimPunctuation(value)) Next End Sub Function TrimPunctuation(ByVal value As String) ' Count leading punctuation. Dim removeFromStart As Integer = 0 For i As Integer = 0 To value.Length - 1 Step 1 If Char.IsPunctuation(value(i)) Then removeFromStart += 1 Else Exit For End If Next ' Count trailing punctuation. Dim removeFromEnd As Integer = 0 For i As Integer = value.Length - 1 To 0 Step -1 If Char.IsPunctuation(value(i)) Then removeFromEnd += 1 Else Exit For End If Next ' Remove leading and trailing punctuation. Return value.Substring(removeFromStart, value.Length - removeFromEnd - removeFromStart) End Function End Module Output One two three four five
Discussion. There are other ways to remove punctuation. The Trim method, for example, can remove a set of characters. You can call Trim with an array of punctuation characters and it will work in the same way.

However: Using a large array may be inefficient. And you may omit characters that Char.IsPunctuation detects.

Char

Regex: Another option is to use the Regex type. This too will often require custom code. It will likely be slower than the example method.

Regex.Match
In my experience, an iterative method that tests characters individually is often the fastest one. Regex methods usually have some overhead. The downside to using For-loops is that it involves more lines of code.For Each, For
Summary. One joy of programming is its variety. There are many ways to accomplish a task. Some ways are better than others, but often the differences are more subtle. This function removes punctuation, is efficient, but has more lines of code.
© 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