1
我想檢查一個字符串是否與數組中的某個字匹配,而不僅僅是匹配字符。 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()
非常感謝你! :) – user2107624 2013-02-27 00:05:19
@ user2107624不要忘記upvote並接受幫助你的答案 – 2013-02-27 00:06:18