Kenny Bones
Kenny Bones

Reputation: 5129

VB.NET - If string contains "value1" or "value2"

I'm wondering how I can check if a string contains either "value1" or "value2"? I tried this:

If strMyString.Contains("Something") Then

End if

This works, but this doesn't:

If strMyString.Contains("Something") or ("Something2") Then

End if

This gives me the error that conversion from string to Long can't be done. If I put the or ("Something2") inside the parenthesis of the first one, it gives me the error that the string cannot be converted to Boolean.

So how can I check if the string contains either "string1" or "string2" without having to write too much code?

Upvotes: 47

Views: 347070

Answers (10)

João Guilherme
João Guilherme

Reputation: 11

If you want to disregard whether the text is uppercase or lowercase, use this:

   If strMyString.ToUpper.Contains("TEXT1") OrElse strMyString.ToUpper.Contains("TEXT2") Then
        'Something
   End if

Upvotes: 1

Dunderchief
Dunderchief

Reputation: 1

Interestingly, this solution can break, but a workaround: Looking for my database called KeyWorks.accdb which must exist:

Run this:

Dim strDataPath As String = GetSetting("KeyWorks", "dataPath", "01", "") 'get from registry

If Not strDataPath.Contains("KeyWorks.accdb") Then....etc.

If my database is named KeyWorksBB.accdb, the If statement will find this acceptable and exit the If statement because it did indeed find KeyWorks and accdb.

If I surround the If statement qualifier with single quotes like 'KeyWorks.accdb', it now looks for all the consecutive characters in order and would enter the If block because it did not match.

Upvotes: -3

KMP
KMP

Reputation: 1

I've approached this in a different way. I've created a function which simply returns true or false.. Usage:

If FieldContains("A;B;C",MyFieldVariable,True|False) then

.. Do Something

End If

Public Function FieldContains(Searchfor As String, SearchField As String, AllowNulls As Boolean) As Boolean

       If AllowNulls And Len(SearchField) = 0 Then Return True

        For Each strSearchFor As String In Searchfor.Split(";")
            If UCase(SearchField) = UCase(strSearchFor) Then
                Return True
            End If
        Next

        Return False

    End Function

Upvotes: 0

Rifky
Rifky

Reputation: 1484

You have to do it like this:

If strMyString.Contains("Something") OrElse strMyString.Contains("Something2") Then
    '[Put Code Here]
End if

Upvotes: 87

Predator
Predator

Reputation: 1275

Here is the alternative solution to check whether a particular string contains some predefined string. It uses IndexOf Function:

'this is your string
Dim strMyString As String = "aaSomethingbb"

'if your string contains these strings
Dim TargetString1 As String = "Something"
Dim TargetString2 As String = "Something2"

If strMyString.IndexOf(TargetString1) <> -1 Or strMyString.IndexOf(TargetString2) <> -1 Then

End If

NOTE: This solution has been tested with Visual Studio 2010.

Upvotes: 7

Matt Wilko
Matt Wilko

Reputation: 27322

In addition to the answers already given it will be quicker if you use OrElse instead of Or because the second test is short circuited. This is especially true if you know that one string is more likely than the other in which case place this first:

If strMyString.Contains("Most Likely To Find") OrElse strMyString.Contains("Less Likely to Find") Then
    'Code
End if

Upvotes: 8

Android
Android

Reputation: 9023

 If strMyString.Tostring.Contains("Something") or strMyString.Tostring.Contains("Something2") Then


     End if

Upvotes: 0

agent-j
agent-j

Reputation: 27923

If strMyString.Contains("Something") or strMyString.Contains("Something2") Then

End if

The error indicates that the compiler thinks you want to do a bitwise OR on a Boolean and a string. Which of course won't work.

Upvotes: 1

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25465

You need this

If strMyString.Contains("Something") or strMyString.Contains("Something2") Then
    'Code
End if

Upvotes: 14

Oded
Oded

Reputation: 499062

You have ("Something2") by itself - you need to test it so a boolean is returned:

If strMyString.Contains("Something") or strMyString.Contains("Something2") Then

Upvotes: 2

Related Questions