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

后端 未结 11 2067
盖世英雄少女心
盖世英雄少女心 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:37

    The drawback of Contains method is that it doesn't allow to specify comparison type which is often important when comparing strings. It is always culture-sensitive and case-sensitive. So I think the answer of WhoIsRich is valuable, I just want to show a simpler alternative:

    listOfStrings.Any(s => s.Equals(myString, StringComparison.OrdinalIgnoreCase))
    

提交回复
热议问题