2012-08-29 76 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

希望这有助于