0

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

genespos
  • 3,211
  • 6
  • 38
  • 70

1 Answers1

4

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)
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263