45

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kenny Bones
  • 5,017
  • 36
  • 111
  • 174

10 Answers10

83

You have to do it like this:

If strMyString.Contains("Something") OrElse strMyString.Contains("Something2") Then
    '[Put Code Here]
End if
Rifky
  • 1,444
  • 11
  • 26
14

You need this

If strMyString.Contains("Something") or strMyString.Contains("Something2") Then
    'Code
End if
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
7

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
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
7

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.

Chris Frederick
  • 5,482
  • 3
  • 36
  • 44
Predator
  • 1,267
  • 3
  • 17
  • 43
2

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

If strMyString.Contains("Something") or strMyString.Contains("Something2") Then
Oded
  • 489,969
  • 99
  • 883
  • 1,009
1
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.

agent-j
  • 27,335
  • 5
  • 52
  • 79
1

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
Chiwda
  • 1,233
  • 7
  • 30
  • 52
0

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
KMP
  • 1
  • Rather than combining and slicing strings, which will incidentally fail if one of the string sought why not pass in the array of sought values as a `params` variable sized arg? You'll allocate far fewer objects, not have escaping problems for your separators and it'll be faster. – Dragonthoughts Jan 14 '21 at 11:56
0
 If strMyString.Tostring.Contains("Something") or strMyString.Tostring.Contains("Something2") Then


     End if
Android
  • 8,995
  • 9
  • 67
  • 108
-3

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578