gpt4 book ai didi

VB.net 在字符串中搜索术语?

转载 作者:行者123 更新时间:2023-12-02 05:32:41 25 4
gpt4 key购买 nike

    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

这是我的代码。我希望它是这样的,如果您输入的字符串包含两个以上的这些术语,那么它会写“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

那是我的新代码。仍然无法正常工作,它的打印测试在第一个输入 corrupt 和第二个 broken 后失败。又要帮忙?非常感谢大家。

最佳答案

为什么这么复杂?只需使用 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

关于VB.net 在字符串中搜索术语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12171030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com