VB.NET - If string contains "value1" or "value2"
Asked Answered
R

10

47

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?

Rocher answered 16/6, 2011 at 9:58 Comment(0)
D
86

You have to do it like this:

If strMyString.Contains("Something") OrElse strMyString.Contains("Something2") Then
    '[Put Code Here]
End if
Duax answered 16/6, 2011 at 10:6 Comment(3)
I'll go ahead and set this as the answer, since you're a newer user :)Rocher
I'd suggest using the short circuited version of Or, OrElse.Conciliator
For clean code please delete the asterisc at the end of strMyString.Contains("Something2")* <<---Dozer
P
14

You need this

If strMyString.Contains("Something") or strMyString.Contains("Something2") Then
    'Code
End if
Papule answered 16/6, 2011 at 10:0 Comment(1)
I can't believe I didn't try this.. And such a quick response! :)Rocher
T
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
Tonguing answered 16/6, 2011 at 13:21 Comment(0)
K
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.

Kanara answered 26/6, 2011 at 21:40 Comment(0)
M
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
Masqat answered 16/6, 2011 at 9:59 Comment(0)
G
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.

Guib answered 16/6, 2011 at 10:0 Comment(0)
U
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
Unadvised answered 5/9, 2022 at 17:59 Comment(0)
I
0
 If strMyString.Tostring.Contains("Something") or strMyString.Tostring.Contains("Something2") Then


     End if
Instructive answered 16/6, 2011 at 10:1 Comment(0)
J
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
Jerri answered 17/6, 2020 at 8:46 Comment(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.Garmaise
W
-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.

Wolframite answered 19/6, 2014 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.