Remove special characters from a string

最后都变了- 提交于 2019-12-18 20:09:29

问题


These are valid characters:

a-z
A-Z
0-9
-
/ 

How do I remove all other characters from my string?


回答1:


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



回答2:


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.




回答3:


Function RemoveCharacter(ByVal stringToCleanUp)
    Dim characterToRemove As String = ""
        characterToRemove = Chr(34) + "#$%&'()*+,-./\~"
        Dim firstThree As Char() = characterToRemove.Take(16).ToArray()
        For index = 1 To firstThree.Length - 1
            stringToCleanUp = stringToCleanUp.ToString.Replace(firstThree(index), "")
        Next
        Return stringToCleanUp
End Function



回答4:


Dim txt As String
txt = Regex.Replace(txt, "[^a-zA-Z 0-9-/-]", "")


来源:https://stackoverflow.com/questions/3701018/remove-special-characters-from-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!