genespos
genespos

Reputation: 3311

vb.net How to check if a string contains one of the values of a key of Dictionary (or array)

I need to check if a string contains one of the values (strings) of an Hashset

I was able to do it, but I've searched for a faster code. I found solutions only for c#, java, phyton but nothing for vb.net

This is a sample of the code I'm using:

    Dim mDict = New Dictionary(Of String, String)
    Dim ArrStr() As String = {"First:", "Second:", "Third:"}
    Dim StringA$ = "Second: It works!"

For Each k As String In ArrStr
    If StringA.Contains(k) Then
        mDict(k) = StringA
        Exit For
    End If
Next k

My question is: is there a way to avoid loop?

EDIT:

I want to thank you all for giving me suggestions and clarifications. I've understood that my question doesn't have answer and maybe it is a bad question.

I haven't got an answer but I've learned something ... Thanks again

Upvotes: 0

Views: 5170

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

You could use LINQ, but it will still perform a loop internally:

mDict = ArrStr.Where(Function(x) StringA.Contains(x))
              .ToDictionary(Function(x) x, Function(x) StringA)

Upvotes: 4

Related Questions