vb.net如何检查一个字符串是否有某个字
问题描述:
我想检查一个字符串是否与数组中的某个字匹配,而不仅仅是匹配字符。 contains方法只检查匹配字符而不是整个单词。这里是我的代码:vb.net如何检查一个字符串是否有某个字
Dim builder As New StringBuilder()
Dim reader As New StringReader(txtOCR.Text)
Dim titles() As String = {"the", "a", "an", "of"}
Dim regex As New Regex(String.Join("|", titles), RegexOptions.IgnoreCase)
While True
Dim line As String = reader.ReadLine()
If line Is Nothing Then Exit While
Dim WordCount = New Regex("\w+").Matches(line).Count
If WordCount = 1 And Not line.ToLower().Contains("by") Then
builder.AppendLine(line)
ElseIf regex.IsMatch(line) Then
builder.AppendLine(line)
End If
End While
txtTitle.Text = builder.ToString()
答
您可以使用单词边界\b
包围的单个单词:
New Regex("\b" & String.Join("\b|\b", titles) & "\b")
非常感谢你! :) – user2107624 2013-02-27 00:05:19
@ user2107624不要忘记upvote并接受帮助你的答案 – 2013-02-27 00:06:18