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

标签 vb.net string console readline

    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”。

---2012 年 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

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

最佳答案

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

相关文章:

JSON反序列化覆盖现有数据表结构

vb.net - 我打印了包含多个 "\"的文本,但 "\S"变成了 "1"

jquery - 使用 CSS 或 jQuery 将 HTML 中的匹配字符串与标题分开

c++ - Arduino int 到字符串的转换

python - 使用控制台启动程序时无法写入文件

python - pydev 交互式控制台总是消失和其他控制台问题

mysql - VB.NET、MySQL 和 Unicode

asp.net-mvc - 返回对象名称为 MVC 的 Json 结果

java - 拆分字符串 ('_' 作为分隔符)

C++ 控制台程序在完成前关闭。