Remove special characters from a string

后端 未结 4 2121
日久生厌
日久生厌 2021-01-04 15:44

These are valid characters:

a-z
A-Z
0-9
-
/ 

How do I remove all other characters from my string?

相关标签:
4条回答
  • 2021-01-04 15:57
    Dim txt As String
    txt = Regex.Replace(txt, "[^a-zA-Z 0-9-/-]", "")
    
    0 讨论(0)
  • 2021-01-04 16:01
    Dim cleanString As String = Regex.Replace(yourString, "[^A-Za-z0-9\-/]", "")
    
    0 讨论(0)
  • 2021-01-04 16:04

    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.

    0 讨论(0)
  • 2021-01-04 16:06
    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
    
    0 讨论(0)
提交回复
热议问题