比较字符串和数组c#

use*_*287 7 .net c# arrays string

true如果字符串包含数组的任何元素,我将如何创建一个返回的函数?

像这样的东西:

string str = "hi how are you";
string[] words = {"hi", "hey", "hello"};
Run Code Online (Sandbox Code Playgroud)

会回来的true.

das*_*ght 9

你可以这样做:

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
}
Run Code Online (Sandbox Code Playgroud)

这种方法并没有要求该元素是一个完整的字(即上述片段将返回true,即使这个词"fox"是不存在有作为一个单词).

  • 更小:`bool b = words.Any(str.Contains);` (7认同)
  • @ user1852287是 - 使用`myStr.IndexOf(s,StringComparison.OrdinalIgnoreCase)`使其不区分大小写. (2认同)