繁体   English   中英

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

[英]How do I check to see if a string is within an array in Visual Basic?

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

我有一个数组:

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

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

使用Contains

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

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

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

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

相反,我使用了IndexOf

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

回答:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM