hewman
hewman

Reputation: 47

if strings contains multiple values

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

Upvotes: 2

Views: 8593

Answers (3)

jmcilhinney
jmcilhinney

Reputation: 54427

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

Upvotes: 5

Silvergerma
Silvergerma

Reputation: 58

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

Upvotes: 0

Rohan Bari
Rohan Bari

Reputation: 7726

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.

Upvotes: 0

Related Questions