sql - 清理VB中的文本字符串

标签 sql vb.net string

我正在尝试从将成为 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/

相关文章:

c# - 程序有时会崩溃

.net - 如何创建 ListView 的 SelectedItem 属性的双向链接?

string - CMutablePointer<CString> to String in swift-language

python - 如何使用 rstrip 删除尾随字符?

c - 错误: result of comparison against a string literal is unspecified (use strncmp instead) and Check50 showing error on credit problem

sql - 在 Delphi 中读取 MS Access mdb 文件(免费)?

MySQL View - 非法混合排序规则

mysql - 如何获取相邻记录?

vb.net - 查找没有属性的元素

mysql - 如何使用复合键作为外键?