String contains another two strings

前端 未结 17 1577
故里飘歌
故里飘歌 2020-12-15 07:12

Is it possible to have the contain function find if the string contains 2 words or more? This is what I\'m trying to do:

string d = \"You hit someone for 50          


        
17条回答
  •  情书的邮戳
    2020-12-15 07:31

    So you want to know if one string contains two other strings?

    You could use this extension which also allows to specify the comparison:

    public static bool ContainsAll(this string text, StringComparison comparison = StringComparison.CurrentCulture, params string[]parts)
    {
        return parts.All(p => text.IndexOf(p, comparison) > -1);
    }
    

    Use it in this way (you can also omit the StringComparison):

    bool containsAll = d.ContainsAll(StringComparison.OrdinalIgnoreCase, a, b);
    

提交回复
热议问题