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

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

    As I needed to check if there are items from a list in a (long) string, I ended up with this one:

    listOfStrings.Any(x => myString.ToUpper().Contains(x.ToUpper()));
    

    Or in vb.net:

    listOfStrings.Any(Function(x) myString.ToUpper().Contains(x.ToUpper()))
    

提交回复
热议问题