我尝试使用这两个代码:
Dim splitQuery() As String = Regex.Split(TextBoxQuery.Text, "\s+")
和
Dim splitQuery() As String = TextBoxQuery.Text.Split(New Char() {" "c})
我的示例查询是"狗。"请注意," dog"之间只有一个空格。和"。"当我检查splitQuery的长度时,它给了我3,分裂的单词是a,dog和"。"
如何阻止它计数"。"和其他符号作为单词?我希望单词/术语(字母数字)只存储在我的splitQuery数组中。感谢。
答案 0 :(得分:0)
你还应该能够创建一个不需要的字符串,并使用stringsplit选项将它们修剪为RemoveEmptyEntries。
dim unwanted as string = "./?!#"
Dim splitQuery() as string = yourString.Trim(unwanted.tochararray).Split(New Char() {" "c}), StringSplitOptions.RemoveEmptyEntries)
答案 1 :(得分:0)
我会分两部分解决这个问题。
我会按照你正在做的空格分割文本
然后我会浏览该字词列表并删除任何非字母数字的查询字词。
以下是一个例子:
Imports System.Collections
' ... Your Other Code ...
' A function to determine if a string is AlphaNumeric
Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
Dim IsAlpha As Boolean = False
If System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+$") Then
IsAlpha = True
Else
IsAlpha = False
End If
Return IsAlpha
End Function
' A function to get the words from the textbox
Private Function GetWords() As String()
' Get a raw list of all words separated by spaces
Dim splitQuery() As String = Regex.Split(TextBoxQuery.Text, "\s+")
' ArrayList to place all words into:
Dim alWords As New ArrayList()
' Loop all words and check them:
For Each word As String In splitQuery
If(IsAlphaNum(word)) Then
' Word is alphanumeric
' Add it to the list of alphanumeric words
alWords.add(word)
End If
Next
' Convert the ArrayList of words to a primitive array of strings
Dim words As String() = CType(alWords.ToArray(GetType(String)), String())
' Return the list of filtered words
return words
End Function
此代码执行以下操作:
ArrayList
ArrayList
。如果它不是字母数字,则忽略该术语。ArrayList
中的术语/单词转换回普通的String数组并返回。由于此解决方案使用ArrayList
,因此需要System.Collections
作为导入。
答案 2 :(得分:0)
我建议分两步完成:
使用txt = Regex.Replace(TextBoxQuery.Text, "\W*$", "", RegexOptions.RightToLeft)
删除字符串末尾的非单词字符
然后,使用\s+
:splits = Regex.Split(txt, "\s+")