Question How to remove all special characters from string?

msivasakthi

Member
Joined
Jan 12, 2009
Messages
6
Programming Experience
Beginner
hi all,

How to remove or replace all special characters(!@#$%^&*(){}[]""_+<>?/) from string in vb.net?

the example string is like like to## remove' all" special^& characters from string

thanks,
 
It depends exactly what you mean by "special characters". The general solution is going to be the same but the specifics may change. If you mean everything that isn't a letter or number then you can do this:
VB.NET:
Dim str As String = "to## remove' all"" special^& characters from string"
Dim sb As New System.Text.StringBuilder

For Each ch As Char In str
    If Char.IsLetterOrDigit(ch) OrElse ch = " "c Then
        sb.Append(ch)
    End If
Next

MessageBox.Show(sb.ToString())
You might look at some of the other members of the Char structure to see what else might be of help.

In your case though, you may just have to create your own list of characters you want to remove because they may not fit neatly into existing categories:
VB.NET:
Dim illegalChars As Char() = "!@#$%^&*(){}[]""_+<>?/".ToCharArray()
Dim str As String = "to## remove' all"" special^& characters from string"
Dim sb As New System.Text.StringBuilder

For Each ch As Char In str
    If Array.IndexOf(illegalChars, ch) = -1 Then
        sb.Append(ch)
    End If
Next

MessageBox.Show(sb.ToString())
 
You can try the following function

Public Function strip(ByVal des As String)
Dim strorigFileName As String
Dim intCounter As Integer
Dim arrSpecialChar() As String = {".", ",", "<", ">", ":", "?", """", "/", "{", "[", "}", "]", "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "|", " ", "\"}
strorigFileName = des
intCounter = 0
Dim i As Integer
For i = 0 To arrSpecialChar.Length - 1

Do Until intCounter = 29
des = Replace(strorigFileName, arrSpecialChar(i), "")
intCounter = intCounter + 1
strorigFileName = des
Loop
intCounter = 0
Next
Return strorigFileName

End Function
 
Thanks fro reply....I have used suggestions.. but > and < symbols are not replaced when it is occurred in string.. how can i replace that also?
 
How about:
VB.NET:
Dim illegalChars As Char() = "!@#$%^&*(){}[]""_+<>?/".ToCharArray()
Dim str As String = "to## remove' all"" special^& characters from string"

For each ch In illegalChars
      str = str.Replace(ch, CChar(""))
Next
 
Here's a regex that might well do what is wanted:

Regex.Replace(YOUR_STRING, "[^0-9a-zA-Z ]+?", "")

Instead of specifying what characters to remove, it specifies what to keep. To read the pattern it means:

at least one sequence of (NOT any characters in the range 0 to 9, a to z, A to Z or space) matched pessimistically. It means the matcher will crawl forwards through the string, looking for sequences of characters that are not letters, numbers or spaces, and replace the entire sequence with 0. You can experiment whether optimistic matching will be faster, but pessimistic would be my first guess

In contrast, to specify what to remove:

Regex.Replace(YOUR_STRING, "[-#'!"£$%^&*()\\]+?", "")

This is harder, because some characters have special meaning.
- is a range indicator, putting it first, disables its range meaning
^ is NOT indicator, putting it OTHER than first disables its NOT matching
\ [ and ] will have to be escaped with \
All other regex special characters lose their meaning in a character class
 
Last edited:
You can try the following function

Public Function strip(ByVal des As String)
Dim strorigFileName As String
Dim intCounter As Integer
Dim arrSpecialChar() As String = {".", ",", "<", ">", ":", "?", """", "/", "{", "[", "}", "]", "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "|", " ", "\"}
strorigFileName = des
intCounter = 0
Dim i As Integer
For i = 0 To arrSpecialChar.Length - 1

Do Until intCounter = 29
des = Replace(strorigFileName, arrSpecialChar(i), "")
intCounter = intCounter + 1
strorigFileName = des
Loop
intCounter = 0
Next
Return strorigFileName

End Function

I know this thread is old but HivelocityDD you really helped me with this code! Thanks!!
 
Hi,

I am also looking for the same.

Do we really need Do Until loop? I think it works fine with just for loop.

Correct me if I am wrong.
 
Back
Top