Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove special characters from a string

These are valid characters:

a-z
A-Z
0-9
-
/ 

How do I remove all other characters from my string?

like image 411
Sriram Avatar asked Sep 13 '10 13:09

Sriram


3 Answers

Dim cleanString As String = Regex.Replace(yourString, "[^A-Za-z0-9\-/]", "")
like image 129
LukeH Avatar answered Sep 19 '22 01:09

LukeH


Dim txt As String
txt = Regex.Replace(txt, "[^a-zA-Z 0-9-/-]", "")
like image 36
But Jao Avatar answered Sep 20 '22 01:09

But Jao


Use either regex or Char class functions like IsControl(), IsDigit() etc. Get a list of these functions here: http://msdn.microsoft.com/en-us/library/system.char_members.aspx

Here's a sample regex example:

(Import this before using RegEx)

Imports System.Text.RegularExpressions

In your function, write this

Regex.Replace(strIn, "[^\w\\-]", "")

This statement will replace any character that is not a word, \ or -. For e.g. aa-b@c will become aa-bc.

like image 37
Sidharth Panwar Avatar answered Sep 21 '22 01:09

Sidharth Panwar