2012-08-29 79 views
3
Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"} 
    Dim terms1 As Integer = 0 
    Dim terms1string As String = "" 
    terms1string = Console.ReadLine() 
    For Each st As String In keys1 
     terms1 = terms1 + 1 
    Next 
    If terms1 < 2 Then 
     Console.WriteLine("yay!") 
    Else 
     Console.WriteLine("YouFail") 
    End If 

Theres my code。我希望它是,如果你輸入的字符串有超過兩個這樣的術語,那麼它寫「Yay」 - 否則它會寫「YouFail」。VB.net搜索字符串中的術語?

---更新12年8月29日---

Function StageTwo(ByVal fname, ByVal lname, ByVal city) 
    Console.WriteLine("Describe the U.S. Government.") 
    Dim overall As Integer = 0 
    Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"} 
    Dim terms1 As Integer = 0 
    Dim terms1string As String = "" 
    terms1string = Console.ReadLine() 
    For Each st As String In keys1 
     If InStr(terms1string, st) > 0 Then '<<<this line right here! 
      terms1 = terms1 + 1 
     End If 
    Next 
    If terms1 < 0 Then 
     Console.WriteLine("yay!") 
     overall = overall + 1 
    End If 
    Console.WriteLine() 
    Console.WriteLine("Describe the economic status in the U.S.") 
    Dim keys2() As String = {"broken", "backed", "failed", "skewed", "tilted", "99%", "rigged", "unfair"} 
    Dim terms2 As Integer = 0 
    Dim terms2string As String = "" 
    terms2string = Console.ReadLine() 
    For Each st As String In keys2 
     If InStr(terms2string, st) > 0 Then '<<<this line right here! 
      terms2 = terms2 + 1 
     End If 
    Next 
    If terms2 < 0 Then 
     Console.WriteLine("yay!") 
     overall = overall + 1 
    End If 
    If overall = 2 Then 
     Console.WriteLine() 
     Console.WriteLine("Enter a username.") 
     Dim username As String = "" 
     username = Console.ReadLine() 
     Console.WriteLine("Please wait.") 
     IsURLValid(username, overall) 
    Else 
     Console.WriteLine("Test Failed.") 
    End If 
    System.Threading.Thread.Sleep(2000) 
End Function 

這是我的新代碼。仍然無法正常工作,它在打印測試失敗後進入第一個腐敗和第二個破碎。再次幫助? 非常感謝你們。

+1

看一看正則表達式。這是一個學習曲線,但是一個明確的投資。 –

回答

2

爲什麼這麼複雜?只需使用Count

Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"} 
Dim terms1string = Console.ReadLine() 

Dim terms1 = keys1.Count(function(key) terms1string like "*" & key & "*") 

If terms1 < 2 Then 
    Console.WriteLine("yay!") 
Else 
    Console.WriteLine("YouFail") 
End If 

如果你想匹配的單個單詞(foobar power lies是2場比賽,foobarpowerlies 0匹配),您可以使用此行:

Dim terms1 = keys1.Count(function(key) terms1string.Split().Contains(key)) 

爲了完整,這是一個正則表達式版本:

' generous match ('foobarpowerlies' => 2 matches) 
Dim pattern = String.Join("|", keys1) 
Dim terms1 = Regex.Matches(terms1string, pattern).Count 

' strict match using word boundaries ('foobarpowerlies' => 0 matches, but 'foobar power lies' => 2 matches) 
Dim pattern = String.Join("|", keys1.Select(function(key) "\b" & key & "\b")) 
Dim terms1 = Regex.Matches(terms1string, pattern).Count 
0

或許過於簡單,但如果你使用IndexOf,你可以改變你的for循環:

If Not String.IsNullOrEmpty(terms1string) Then 
     For Each st As String In keys1 
      If terms1string.IndexOf(st) <> -1 Then 
       terms1 = terms1 + 1 
      End If 
     Next 
    End If 

這是因爲它不令牌化輸入簡單的......所以像「腐敗」字和「belies」將註冊一個匹配項。如果您需要完全匹配,請查看String.Split以獲取輸入單詞,然後有多個算法選項可將該列表與您的密鑰列表進行比較。

1

我有一些東西給你。

你父親的INSTR()。這是QuickBasic 4.5黑客的武器。不像正則表達式那樣笨拙或隨機;一個更文明的時代的優雅武器。

Module Module1 

    Sub Main() 
    Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"} 
    Dim terms1 As Integer = 0 
    Dim terms1string As String = "" 
    terms1string = Console.ReadLine() 
    For Each st As String In keys1 
     If InStr(terms1string, st) > 0 Then '<<<this line right here! 
     terms1 = terms1 + 1 
     End If 
    Next st 
    If terms1 < 2 Then 
     Console.WriteLine("yay!") 
    Else 
     Console.WriteLine("YouFail") 
    End If 
    Console.ReadKey() 

    End Sub 

End Module 
2

「Austin Powers」是否應該匹配「power」並且應該「uncorrupt」匹配「corrupt」?假設「否」
「POWER」是否與「power」匹配?假設「是」

做到這一點,最安全的方法是使用正則表達式

Function WordCount(keys() As String, terms As String) As Integer 
    Dim pattern As String = "\b(" + Regex.Escape(keys(0)) 
    For Each key In keys.Skip(1) 
     pattern += "|" + Regex.Escape(key) 
    Next 
    pattern += ")\b" 

    Return Regex.Matches("terms", pattern, RegexOptions.IgnoreCase).Count 
End Function 


Sub Main() 
    Dim keys1() As String = {"corrupt", "selfish", "power", "lying", "lies", "media"} 
    Dim count As Integer 
    count = WordCount(keys1, "lying son of a corrupt . . .") ' returns 2 
    count = WordCount(keys1, "Never caught lying and uncorrupt . . .") ' returns 1 
End Sub 

Regex.Escape功能確保在你的鑰匙任何正則表達式的特定字符將被轉義,和正則表達式的命令將不會被處理。

RegexOptions.IgnoreCase選項指示它執行不區分大小寫的匹配。

\b是一個字邊界,所以在匹配前後必須有字邊界(空格,標點符號,新行,字符串開頭,字符串結尾等)。

把鑰匙,這種結構(key1|key2|key3)說,它可以匹配key1key2key3

希望這有助於