Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vb.net

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?

like image 956
Matt Elhotiby Avatar asked Jan 27 '12 19:01

Matt Elhotiby


People also ask

How do you check if a string is present in an array?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

How do you check if a string contains a character in Visual Basic?

In visual basic, the string Contains method is useful to check whether the specified substring exists in the given string or not and it will return a boolean value. In case, if substring exists in a string, then the Contains method will return true otherwise it will return false.

What is an array in Visual Basic?

An array is a set of values, which are termed elements, that are logically related to each other. For example, an array may consist of the number of students in each grade in a grammar school; each element of the array is the number of students in a single grade.

What is static array in VB?

Static and dynamic arraysStatic arrays must include a fixed number of items, and this number must be known at compile time so that the compiler can set aside the necessary amount of memory. You create a static array using a Dim statement with a constant argument: ' This is a static array.


3 Answers

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 
like image 114
vcsjones Avatar answered Sep 21 '22 07:09

vcsjones


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 
like image 42
rbassett Avatar answered Sep 21 '22 07:09

rbassett


Answer:

Dim things As String() = {"a", "b", "c"}
If things.Contains("a") Then
    ' do things
Else
    ' don't do things
End If
like image 26
codingg Avatar answered Sep 18 '22 07:09

codingg