I had a need to find a string like function similar to MSSQL and found this useful function.
public static bool StringLike(this string toSearch, string toFind) { return new Regex(@"A" + new Regex(@".|$|^|{|[|(|||)|*|+|?|").Replace(toFind, ch => @"" + ch).Replace('_', '.').Replace("%", ".*") + @"z", RegexOptions.Singleline).IsMatch(toSearch); }
bool willBeTrue = StringLike("abcdefg","abcd_fg"); bool willAlsoBeTrue = StringLike("abcdefg","ab%f%"); bool willBeFalse = StringLike("abcdefghi","abcd_fg");
Reference: http://stackoverflow.com/questions/5417070/c-sharp-version-of-sql-like
Last Updated on October 26, 2015
You must be logged in to post a comment.