string 从字符串中删除特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3701018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:50:30  来源:igfitidea点击:

Remove special characters from a string

vb.netstringnewline

提问by Sriram

These are valid characters:

这些是有效字符:

a-z
A-Z
0-9
-
/ 

How do I remove all other characters from my string?

如何从我的字符串中删除所有其他字符?

回答by LukeH

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

回答by Sidharth Panwar

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

使用正则表达式或 Char 类函数,如 IsControl()、IsDigit() 等。在此处获取这些函数的列表:http: //msdn.microsoft.com/en-us/library/system.char_members.aspx

Here's a sample regex example:

这是一个示例正则表达式示例:

(Import this before using RegEx)

(在使用 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.

此语句将替换任何不是单词、\ 或 - 的字符。例如,aa-b@c 将变成 aa-bc。

回答by But Jao

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

回答by Anand Vishwakarma

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