Check if a string contains an element from a list (of strings)

后端 未结 11 2059
盖世英雄少女心
盖世英雄少女心 2020-11-27 11:09

For the following block of code:

For I = 0 To listOfStrings.Count - 1
    If myString.Contains(lstOfStrings.Item(I)) Then
        Return True
    End If
Next         


        
11条回答
  •  温柔的废话
    2020-11-27 11:30

    If speed is critical, you might want to look for the Aho-Corasick algorithm for sets of patterns.

    It's a trie with failure links, that is, complexity is O(n+m+k), where n is the length of the input text, m the cumulative length of the patterns and k the number of matches. You just have to modify the algorithm to terminate after the first match is found.

提交回复
热议问题