i have been trying to remove special characters. i am not able to remove many crlf in middile of the string. please help. i got this issue in production, need to resolve it soon. thanks in advance.
views:
83answers:
3
+1
A:
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.
Sidharth Panwar
2010-09-13 13:54:43
+1 The OP probably wants to keep spaces as well - though maybe not tabs (?) which would be `Regex.Replace(strIn, "[^\w\\- ]", "")`
El Ronnoco
2010-09-13 14:20:10
Watch out! The `\w` class doesn't only match alphanumerics. For example, your regex won't correctly clean *"a_string_containing_underscores"*. (Also, the OP wants to allow forward-slashes, not backslashes.)
LukeH
2010-09-13 14:26:19
Hmm. Slash should be different.
Sidharth Panwar
2010-09-13 16:36:07
A:
Try using the Regex.Replace()
method: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace.aspx
Bernard
2010-09-13 13:54:44
+1
A:
Dim cleanString As String = Regex.Replace(yourString, "[^A-Za-z0-9\-/]", "")
LukeH
2010-09-13 14:17:36