Extract all punctuation marks from string in VB.net
How to remove all punctuation marks from a string in vb.net? I really don't want to do stringname.Replace("$", "")
punctuation for every bit, although it will work.
How to do it quickly and efficiently?
Apart from coding the code that encodes this for me ....
You can use a regex to match whatever you want to remove:
str = Regex.Replace(str, "[^A-Za-z]+", String.Empty);
[^...]
- a negative set that matches any character that is not in the set. You can simply add any character you want to keep there.
A quick example using a positive regex. Just put the characters you want to remove in it:
Imports System.Text.RegularExpressions
Dim foo As String = "The, Quick brown fox. Jumped over the Lazy Dog!"
Console.WriteLine(Regex.Replace(foo,"[!,.\"'?]+", String.Empty))
If you want a non-regex you can try something like this:
Dim stringname As String = "^^^%%This,,,... is $$my** original(((( stri____ng."
Dim sb As New StringBuilder
Dim c As Char
For Each c In stringname
If Not (Char.IsSymbol(c) OrElse Char.IsPunctuation(c)) Then
sb.Append(c)
End If
Next
Console.WriteLine(sb.ToString)
Exit " This is my original string
".