Skip to content

Instantly share code, notes, and snippets.

@jdmonty
Last active December 26, 2015 19:59
Show Gist options
  • Save jdmonty/7205438 to your computer and use it in GitHub Desktop.
Save jdmonty/7205438 to your computer and use it in GitHub Desktop.
c# string extension. I like 'like'
public static bool Like(this string @string, string pattern, char wildcard = '%', bool ignoreCase = true)
{
pattern = pattern.Replace(wildcard.ToString(), ".*");
var options = RegexOptions.Singleline | RegexOptions.Compiled;
if (ignoreCase) options |= RegexOptions.IgnoreCase;
var regex = new Regex(pattern, options);
return regex.IsMatch(@string);
}
@jdmonty
Copy link
Author

jdmonty commented Oct 28, 2013

var username = "foobar";
username.Like("foo%"); // yep
username.Like("bar%");  // nope
username.Like("%baz"); // nope
username.Like("%bar"); // yep
username.Like("%oob%"); // yep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment