【问题标题】:How do I check to see if a string is within an array in Visual Basic?如何检查字符串是否在 Visual Basic 中的数组中?
【发布时间】:2020-01-15 23:59:06
【问题描述】:

我是一名 PHP 开发人员,而不是 Visual Basic 人员。

我有一个数组:

Dim ShippingMethod() As String = {"Standard Shipping", "Ground EST"}
Dim Shipping as String = "Ground EST"

如何执行if 语句来检查字符串Shipping 是否在ShippingMethod() 数组中?

【问题讨论】:

标签: vb.net


【解决方案1】:

使用Contains:

If ShippingMethod.Contains(Shipping) Then
    'Go
End If

这意味着区分大小写。如果你想不区分大小写:

If ShippingMethod.Contains(Shipping, StringComparer.CurrentCultureIgnoreCase) Then
    'Go
End If

【讨论】:

    【解决方案2】:

    如果我尝试上述答案,我会收到错误 'Contains' is not a member of 'String()'

    我使用了IndexOf

    Dim index As Integer = Array.IndexOf(ShippingMethod, Shipping)
    If index < 0 Then
        ' not found
    End If
    

    【讨论】:

      【解决方案3】:

      答案:

      Dim things As String() = {"a", "b", "c"}
      If things.Contains("a") Then
          ' do things
      Else
          ' don't do things
      End If
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-18
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        相关资源
        最近更新 更多