【问题标题】:Issue with using the String.Contains()使用 String.Contains() 的问题
【发布时间】:2021-10-11 16:12:41
【问题描述】:

我对编程比较陌生,并且开始使用 Microsoft VB Studio 2019 的 VB.net。我通常使用 Python,因此充分利用了

> If String in("y","yes","YES"):

语句,因此我不必单独将字符串与每个项目进行比较。

一段时间以来,我一直在尝试在 Virtual Basic 上执行此操作,但甚至没有设法获得 1 个值来与要工作的字符串进行比较。我尝试了 2 种不同的方法,第一种只是一个基本的 String.Contains() 命令,我已经这样设置了:

Dim UserSelection As String
Console.Write("Play again? ")
UserSelection = Console.Read()
If UserSelection.Contains("n") = True Then
    UserPlaying = False
End If

我的想法是计算机会查看 UserSelection,如果它在任何时候都包含字母“n”,那么它会导致结果为 True(例如:如果 UserSelection = 'no', 'nope', ' n' ext ext) 但是,每次我运行此代码时,结果总是返回为 false,无论 UserSelection 是什么。

我也尝试过使用 IndexOf 命令(它使搜索不区分大小写)来查看它是否可以工作,但似乎又出现了问题:

Dim UserSelection As String
Console.Write("Play again? ")
UserSelection = Console.Read()
Dim subtxt As String = "n"
Dim comp As StringComparison = StringComparison.OrdinalIgnoreCase
Dim result As Boolean = If(UserSelection.IndexOf(subtxt, comp) > 0, True, False)
If result = True Then
    UserPlaying = False
End If

我的缩进在两个代码块中看起来都是正确的,我终其一生都无法弄清楚这里出了什么问题。

如果有人可以帮我解决这个问题(尤其是如果您可以调整代码以便它可以进行多重比较),那将不胜感激。

非常感谢, 阿尔菲 :)

【问题讨论】:

  • 设置Option Strict ON 并使用Console.ReadKey()。 (例如,Console.Write("Play again? (Y/N)") Dim keyPressed = Console.ReadKey() If keyPressed.Key = ConsoleKey.Y Then ... End If
  • Console.Read() 从控制台读取单个字符并返回一个整数变量(该字符的 Int32 值)。使用Console.ReadLine。开始使用 VB.NET 时应该做的第一件事:在项目设置中将 Option Strict 和 Option Explicit 设置为 ON。否则会掉入很多陷阱,学不好类型系统。
  • Console.Read() 根据文档 docs.microsoft.com/en-us/dotnet/api/… 返回一个 int。我想这会与.Contains() 混淆。
  • 我不同意使用Console.ReadLine 的建议。对于你在做什么,我推荐Console.ReadKey。您可以检查“y”的代码(无论它是否被转移,它都会注册)。 (另外,请注意,缩进在 VB 中并不重要,但 IDE 非常适合为您自动维护缩进。)
  • 这就是您需要使用实际调试器实际调试代码的原因。如果你这样做了,那么你会发现UserSelection 不是你所期望的那样。在继续之前,您应该停止正在做的事情并学习如何使用调试器。

标签: string vb.net contains indexof


【解决方案1】:

我刚刚在 Contains 之前将 .ToLower 添加到 UserSelection 字符串中,因此 N 或 n 将被识别。

Private UserPlaying As Boolean
Sub Main()
    Console.Write("Play again? ")
    Dim UserSelection = Console.ReadLine()
    If UserSelection.ToLower.Contains("n") = True Then
        Debug.Print("It contains n")
        UserPlaying = False
    Else
        Debug.Print("No n")
        UserPlaying = True
    End If
    Console.ReadKey()
End Sub

【讨论】:

    【解决方案2】:

    Console.Read 返回一个整数 (documentation),所以它自然会返回 false。您正在寻找的方法是 Console.ReadLine (documentation),它返回一个字符串。

    看看这个例子:

    Console.Write("Play again? ")
    Dim input = Console.ReadLine()
    Dim stopPlaying = input.IndexOf("n", StringComparison.OrdinalIgnoreCase) > -1
    
    If (stopPlaying) Then
        ' the user replied with "n", "no", "nope", etc.
    Else
        ' the user replied with something that did not contain the letter "n"
    End If
    

    小提琴:https://dotnetfiddle.net/Fihq02

    【讨论】:

    • 如果他不想以不区分大小写的方式进行比较,他也可以使用String.Contains
    • @TimSchmelter - 根据他的问题,看起来他确实想比较忽略大小写。
    • Console.ReadKey
    猜你喜欢
    • 2011-11-02
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多