我有一个字符串和一个字符串列表:
string motherString = "John Jake Timmy Martha Stewart";
我想查找该字符串是否包含列表中的任何字符串,即:
var children = new List<string>{"John", "Mike", "Frank"};
所以我想知道 motherString 是否包含来自 children 的项目之一,即。 '约翰'
解决这个问题的最佳方法是什么?
最佳答案
我能想到的最简单的代码是:
var hasAny = children.Any(motherString.Contains);
如果您希望每个单词都用空格分隔,那么您可以使用:
var hasAny = motherString.Split(new[] { ' ' }).Any(children.Contains);
如果 motherString 中的单词可以用其他字符分隔,则可以这样添加:
motherString.Split(new[] { ' ', ',', ':' })
关于c# - 如何查找字符串是否包含字符串列表的任何项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4987873/