Remove special characters from a string

2019-02-12 14:05发布

These are valid characters:

a-z
A-Z
0-9
-
/ 

How do I remove all other characters from my string?

4条回答
仙女界的扛把子
2楼-- · 2019-02-12 14:10
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
查看更多
该账号已被封号
3楼-- · 2019-02-12 14:18

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.

查看更多
Juvenile、少年°
4楼-- · 2019-02-12 14:25
Dim txt As String
txt = Regex.Replace(txt, "[^a-zA-Z 0-9-/-]", "")
查看更多
劫难
5楼-- · 2019-02-12 14:26
Dim cleanString As String = Regex.Replace(yourString, "[^A-Za-z0-9\-/]", "")
查看更多
登录 后发表回答