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

Mat*_*iby 17 vb.net

我是PHP开发人员而不是Visual Basic人员.

我有一个数组:

Dim ShippingMethod() As String = {"Standard Shipping", "Ground EST"}
Dim Shipping as String = "Ground EST"
Run Code Online (Sandbox Code Playgroud)

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

vcs*_*nes 38

用途Contains:

If ShippingMethod.Contains(Shipping) Then
    'Go
End If
Run Code Online (Sandbox Code Playgroud)

这意味着区分大小写.如果您想要不区分大小写:

If ShippingMethod.Contains(Shipping, StringComparer.CurrentCultureIgnoreCase) Then
    'Go
End If
Run Code Online (Sandbox Code Playgroud)


rba*_*ett 9

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

相反,我使用了IndexOf

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