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我的代码。我希望如果你输入的字符串中有超过两个这样的术语,那么就会写出“Yay” - 否则就会写出“YouFail”。
---更新8/29/12 ---
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
这是我的新代码。仍然没有工作,它的打印测试失败后输入腐败的第一个和破坏的第二个。再帮一次? 非常感谢你们。
答案 0 :(得分: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
答案 1 :(得分:2)
“Austin Powers”应该匹配“权力”并且应该“未损坏”匹配“腐败”吗?假设“不” “POWER”应该匹配“power”吗?假设“是”
最安全的方法是使用Regex
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)
表示它可以匹配key1
或 key2
或 key3
希望这有帮助
答案 2 :(得分: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
答案 3 :(得分: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
它的简单之处在于它不会对输入进行标记化......所以“腐败”和“束缚”这样的词会注册匹配。如果您需要完全匹配,请查看String.Split以获取输入字,然后有许多算法选项可将该列表与您的密钥列表进行比较。