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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9038637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:54:15  来源:igfitidea点击:

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

vb.net

提问by Matt Elhotiby

I am a PHP developer and not a Visual Basic person.

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

I have an array:

我有一个数组:

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

How do I do an ifstatement that will check if the string Shippingis in the ShippingMethod()array?

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

回答by vcsjones

Use Contains:

使用Contains

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

That implies case-sensitivity. If you want case insensitive:

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

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

回答by rbassett

I get the error 'Contains' is not a member of 'String()'if I try the above answer.

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

Instead I used IndexOf:

相反,我使用了IndexOf

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

回答by codingg

Answer:

回答:

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