How to find if a string contains any items of an List of strings?

前端 未结 1 1412
野趣味
野趣味 2020-12-30 04:16

I have a string and a List of strings:

string motherString = \"John Jake Timmy Martha Stewart\";

and I want to find if that string contains

相关标签:
1条回答
  • 2020-12-30 04:32

    The simplest code I could come up with would be:

    var hasAny = children.Any(motherString.Contains);
    

    If you expect each of the words to be seperated by a space then you could use this:

    var hasAny = motherString.Split(new[] { ' ' }).Any(children.Contains);
    

    If the words in motherString could be seperated by other characters, you could add them like this:

    motherString.Split(new[] { ' ', ',', ':' })
    
    0 讨论(0)
提交回复
热议问题