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?
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.
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.
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.
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.
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
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
Answer:
Dim things As String() = {"a", "b", "c"}
If things.Contains("a") Then
' do things
Else
' don't do things
End If
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With