String - ContainsAny
A string extension method that query if the string object contains any values.
public static void Main() { string text = @"<!DOCTYPE html> <html> <body> <h1>This is <b>bold</b> heading</h1> <p>This is <u>underlined</u> paragraph</p> <h2>This is <i>italic</i> heading</h2> </body> </html> "; string [] values = {"<body>", "<h3>"}; // C# Extension Method: String - ContainsAny if(text.ContainsAny(values)) { Console.WriteLine("String found."); } }
View Source
using System; public static partial class Extensions { /// <summary> /// A string extension method that query if '@this' contains any values. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="values">A variable-length parameters list containing values.</param> /// <returns>true if it contains any values, otherwise false.</returns> public static bool ContainsAny(this string @this, params string[] values) { foreach (string value in values) { if (@this.IndexOf(value) != -1) { return true; } } return false; } /// <summary> /// A string extension method that query if '@this' contains any values. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="comparisonType">Type of the comparison.</param> /// <param name="values">A variable-length parameters list containing values.</param> /// <returns>true if it contains any values, otherwise false.</returns> public static bool ContainsAny(this string @this, StringComparison comparisonType, params string[] values) { foreach (string value in values) { if (@this.IndexOf(value, comparisonType) != -1) { return true; } } return false; } }