Remove punctuation. In programs, strings often contain unneeded characters. In VB.NET there are many ways to remove punctuation characters from strings.
IsPunctuation. Here we use the Char.IsPunctuation function. It handles all punctuation characters. It is built into .NET and returns a Boolean telling us whether the char is punctuation.
Example. We introduce TrimPunctuation. This counts punctuation characters at the start and the end. We use 2 for-loops and call Char.IsPunctuation to check each character.
Detail We use the Substring function 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.
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 ModuleOne
two
three
four
five
Discussion. There are other ways to remove punctuation—Trim() can remove a set of characters. You can call Trim with an array of punctuation characters and it will work in the same way.
Performance note. An iterative method that tests characters individually is often the fastest one. Regex has overhead. The downside to using For-loops is that it involves more lines of code.
Summary. This function removes punctuation and is efficient. Some ways to remove punctuation are better than others—and testing for correctness is important.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 19, 2022 (rewrite).