如何检查字符串是否在Visual Basic中的数组中?

时间:2012-01-27 19:03:35

标签: vb.net

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

我有一个数组:

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

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

3 个答案:

答案 0 :(得分:36)

使用Contains

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

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

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

答案 1 :(得分:1)

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

相反,我使用了IndexOf

Dim index As Integer = Array.IndexOf(ShippingMethod, Shipping)
if index < 0 then
    ' not found
endif

答案 2 :(得分:0)

答案:

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