如果字符串包含数组的任何元素,我将如何制作一个返回 true
的函数?
像这样:
string str = "hi how are you";
string[] words = {"hi", "hey", "hello"};
将返回 true
。
最佳答案
你可以这样做:
var array = new[] {"quick", "brown", "fox"};
var myString = "I love foxes.";
if (array.Any(s => myStr.IndexOf(s) >= 0)) {
// One of the elements is there
}
这种方法不要求元素是一个完整的单词(即上面的片段将返回true
,即使单词“fox”
不作为一个单词出现)。
关于c# - 比较字符串和数组c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558576/