proposal: String.ContainsAny()

Jan 28, 2011 at 2:26 PM
Edited Jan 28, 2011 at 2:40 PM

I have a proposal. How about ContainsAny for string, which checks, if one of the given strings is contained in the "this" string (even null/empty values):

        ///<summary>
        /// Returns true, if the text contains at least one given subString
        ///</summary>
        ///<param name="s"></param>
        ///<param name="subStrings"></param>
        ///<returns></returns>
        public static bool ContainsAny(this string sparams string[] subStrings)
        {
            var terms = subStrings.ToList();
            if (terms.Contains(""))
            {
                if (s.IsNullOrEmpty()) return true;
                terms.Remove("");
            }
            return terms.Any(s.Contains);
        }
Requires LinQ.

Test:

var text = "hallo enum!";
 var ok = (text.ContainsAny("""hallo""hello", "bonjour"));

 

Developer
Nov 27, 2012 at 10:32 AM

humbrie,

The following extensions were added.

String.ContainsAny(String[])
String.ContainsAny(StringComparison, String[])

String.ContainsAll(String[])
String.ContainsAll(StringComparison, String[])

String.EqualsAny(StringComparison, String[])