Method I Wish Was There: Regex

I don’t want to have to pull in System.Text.RegularExpression every time I want a new Regex. Perl, boo, awk and javascript just say // for regex. In C#, I can just say “abc\d{3}”.Regex().

public static System.Text.RegularExpressions.Regex Regex(this string source)
{
  return new System.Text.RegularExpressions.Regex(source);
}

Now I can write:

if (“there are (\d+) lights”.Regex().Match().Groups[1].Value > 0) …

Method I Wish Was There: string.Contains

Sure, string has a member method of Contains, but its an exact comparison. What I really want is something that takes a StringComparison argument as well.

public static bool Contains(this string source, string search, StringComparison comparison)
{
  return source.IndexOf(source, 0, comparison) > -1;
}

Not much to this extension method, but it gives me the semantics that I want.

if (foo.Contains("bar", StringComparison.CurrentCultureIgnoreCase)) …