我有一个字符串清单

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/

10-12 12:43
查看更多