views:

83

answers:

3

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.

+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
+1 The OP probably wants to keep spaces as well - though maybe not tabs (?) which would be `Regex.Replace(strIn, "[^\w\\- ]", "")`
El Ronnoco
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
Hmm. Slash should be different.
Sidharth Panwar
A: 

Try using the Regex.Replace() method: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace.aspx

Bernard
+1  A: 
Dim cleanString As String = Regex.Replace(yourString, "[^A-Za-z0-9\-/]", "")
LukeH