views:

678

answers:

3

The title says it all. How do I strip all punctuation from a string in vb.net? I really do not want to do stringname.Replace("$", "") for every single bit of punctuation, though it would work.

How do i do this quickly and efficiently?

Other than coding something that codes this for me....

+3  A: 

Quick example using a positive regex match. Simply place the characters you want removed 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))
FlySwat
I'd use a negative match, not a positive match, personally. :)
Guffa
It worked! I think I need to escape my " with "" though, so I did.I can simply add more characters inside the brackets right? How will I strip brackets then?
Cyclone
Escape with a \ in C#, I don't know about VB.
FlySwat
LOL Guffa. Mine has merit in that the Regex is shorter and easier to modify.
FlySwat
Er, how do I escape a \ then? I really want to strip ALL punctuation other than spaces.
Cyclone
Google for VisualBasic string escape rules.
FlySwat
@FlySwat: Well, it's shorter until you have added all the punctuation characters, not just a few of them. It has already proven harder to modify as some punctuation characters needs to be escaped...
Guffa
Yeah, I found people talking about this from '03, nothing that worked D:
Cyclone
A: 

You can use a regular expression to match anything that you want to remove:

str = Regex.Replace(str, "[^A-Za-z]+", String.Empty);

[^...] is a negative set that matches any character that is not in the set. You can just put any character there that you want to keep.

Guffa
I'd use a positive match, not a negative match, personally.
FlySwat
It says Regex was undeclared
Cyclone
You'll need to use Imports System.Text.RegularExpressions
FlySwat
Right, why on earth doesnt that import when I imported System.Text lol?
Cyclone
@Cyclone: Importing is not cascading. If you import the System namespace that doesn't import everything in the entire framework...
Guffa
Sorry, im half used to Flash where import statements can cascade, and half used to VB. I need to get whole used to both lol....
Cyclone
Okay wait, can yours *not* strip spaces?
Cyclone
Of course, just put a space in the negative set: "[^A-Za-z ]+"
Guffa
Oh. Lol, okay...I will give that a shot....
Cyclone
Alright, this wound up working since I couldnt figure out how to escape those characters! Thanks!
Cyclone
A downvote? If you don't say what you don't like then it's rather pointless...
Guffa
A: 

If you want a non-regex solution, you could 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)

Output is "This is my original string".

bobbymcr