24

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

I have an array:

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

How do I do an if statement that will check if the string Shipping is in the ShippingMethod() array?

1

3 Answers 3

52

Use 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
Sign up to request clarification or add additional context in comments.

Comments

13

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

Instead I used IndexOf :

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

Comments

3

Answer:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.