2

i have Over 40 values to check if my string contains one of them or not

and

if myString.Contains("value1") or string.Contains("value2") or string.Contains("value3") 'etc...' then
end if

is not convenient at all for me , how i can put multiple values inside Contains() ?

and if something is better than Contains() it would be alot better

6
  • what language are you using? because the code shown is neither c# nor vb Commented May 15, 2020 at 7:27
  • @MarcGravell, given that the question is tagged VB.NET and the syntax is VB, I think it's safe to say that it's VB.NET. The OP obviously typed that code rather than copying valid code from the IDE but it's clearly VB. Commented May 15, 2020 at 7:32
  • @jmcilhinney the VB compiler may disagree with that assessment Commented May 15, 2020 at 7:33
  • @MarcGravell, I didn't say that the code would compile. In fact, I implied that it wouldn't by saying that it was obviously typed directly rather than copied. It's obviously meant to be VB though, especially given the VB.NET tag. Commented May 15, 2020 at 7:35
  • @MarcGravell, that said, I was dubious about your C# answer but, if you're going to tag a question .NET, you're implying that it's a .NET issue and you'll accept a .NET answer. Your C# answer illustrates the .NET principles perfectly well so is a perfectly acceptable answer. If you can write in VB though, I would probably tend to do so. You'd obviously already answered before realising the language issue though, so I wouldn't hold it against you if you didn't. Commented May 15, 2020 at 7:38

3 Answers 3

5

Contains will only accept a single value so you have to call it once for each value. The trick is to write your code such that you only write that call once but it gets executed multiple times. In the age of LINQ, you would do that like this:

Dim values = {"value1", "value2", "value3"}

If values.Any(Function(s) myString.Contains(s)) Then
    '...
End If

The old-school option would be using a loop:

Dim values = {"value1", "value2", "value3"}
Dim containsAny = False

For Each value in values
    If myString.Contains(value) Then
        containsAny = True
        Exit For
    End If
Next

If containsAny Then
    '...
End If

You could put either of those into a method:

Public Function ContainsAny(text As String, ParamArray values As String()) As Boolean
    Return values.Any(Function(s) text.Contains(s))
End Function
Public Function ContainsAny(text As String, ParamArray values As String()) As Boolean
    For Each value in values
        If text.Contains(value) Then
            Return True
        End If
    Next

    Return False
End Function

and then call it like so:

Dim values = {"value1", "value2", "value3"}

If Me.ContainsAny(myString, values) Then
    '...
End If

or like so:

If Me.ContainsAny(myString, "value1", "value2", "value3") Then
    '...
End If

You could also write extension methods like so:

Imports System.Runtime.CompilerServices

Module StringExtensions

    <Extension>
    Public Function ContainsAny(source As String, ParamArray values As String()) As Boolean
        Return values.Any(Function(s) source.Contains(s))
    End Function

End Module

or like so:

Imports System.Runtime.CompilerServices

Module StringExtensions

    <Extension>
    Public Function ContainsAny(source As String, ParamArray values As String()) As Boolean
        For Each value In values
            If source.Contains(value) Then
                Return True
            End If
        Next

        Return False
    End Function

End Module

and then call it like so:

Dim values = {"value1", "value2", "value3"}

If myString.ContainsAny(values) Then
    '...
End If

or like so:

If myString.ContainsAny("value1", "value2", "value3") Then
    '...
End If
Sign up to request clarification or add additional context in comments.

Comments

0

If you just want to check if a word exists in a specified string, then use the following:

Dim str As String = "Hello World Welcome to Stack Overflow"
Dim wordsToFind() As String = {"Hello", "to"}

For Each word In wordsToFind
    If str.Contains(word) Then
        MsgBox("One of the word found!")
        Return
    End If
Next

If any word is found in str from wordsToFind array, then it'll show a message and returns.

Comments

0

if you need to check for this more then once you could also add an extentions methode to check if a string contains any of the given params.

for example

Public Function ContainsAnyOf(Of T)(SourceList As List(Of T), ParamArray ParamList() As T) As Boolean
    If SourceList IsNot Nothing AndAlso ParamList IsNot Nothing Then
        For Each ParamItem As T In ParamList
            If SourceList.Contains(ParamItem) Then Return True
        Next
    End If
    Return False
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.