我正在尝试从将成为 SQL 查询一部分的文本字段中清理字符串。
我创建了一个函数:
Private Function cleanStringToProperCase(dirtyString As String) As String
Dim cleanedString As String = dirtyString
'removes all but non alphanumeric characters except @, - and .'
cleanedString = Regex.Replace(cleanedString, "[^\w\.@-]", "")
'trims unecessary spaces off left and right'
cleanedString = Trim(cleanedString)
'replaces double spaces with single spaces'
cleanedString = Regex.Replace(cleanedString, " ", " ")
'converts text to upper case for first letter in each word'
cleanedString = StrConv(cleanedString, VbStrConv.ProperCase)
'return the nicely cleaned string'
Return cleanedString
End Function
但是当我尝试用两个单词清理任何文本时,它会删除所有空白。 “daz's bike”变成“Dazsbike”。我假设我需要修改以下行:
cleanedString = Regex.Replace(cleanedString, "[^\w\.@-]", "")
这样它也可以保留单个空白字符。关于如何执行此操作的建议很受欢迎,因为我在任何在线教程或 MSDN 网站 ( http://msdn.microsoft.com/en-us/library/844skk0h(v=vs.110).aspx ) 上都找不到它
最佳答案
使用“[^\w\.,@\-\']”
代替模式字符串。
另外,我会使用
Regex.Replace(cleanedString, " +", " ")
而不是
Regex.Replace(cleanedString, " ", " ")
关于sql - 清理VB中的文本字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16545277/