我有一个字符串清单
string[] arr = new string[] { "hello world", "how are you", "what is going on" };
我需要检查我提供的字符串是否使用了
arr
字符串之一中的每个单词所以说我有
string s = "hello are going on";
这将是一个匹配,因为
s
中的所有单词都在arr
中的一个字符串中string s = "hello world man"
这不会是一个匹配项,因为
arr
中的任何字符串中都没有“ man”我知道如何编写“较长”方法来执行此操作,但是我可以编写一个不错的linq查询吗?
最佳答案
string[] arr = new string[] { "hello world", "how are you", "what is going on" };
string s = "hello are going on";
string s2 = "hello world man";
bool bs = s.Split(' ').All(word => arr.Any(sentence => sentence.Contains(word)));
bool bs2 = s2.Split(' ').All(word => arr.Any(sentence => sentence.Contains(word)));
关于c# - 如何检查字符串列表是否匹配词组中的所有单词?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15394057/