Mark Perera Mark Perera - 1 year ago 106
SQL Question

Matching a Pattern in C#

This may seem like an obvious thing, but I want to know know if there are wildcards in C# for matching strings.
I want to write a program so that users can search for something based on the date value in the database. What I have so far works as long as the whole date is entered, but entering half a date causes a parsing error
I'm using this code now

" ... where Date like " DateTime.Parse(textbox.text.trim()) " + %;"

I want to see if there is an way to see what the user input (only the year, or month and year without day, or for it not to crash if only half the year is entered)

A solution that involves doing this in SQL itself doesn't matter

Answer Source

Solution 1:

public static class MyStringExtensions
  public static bool Like(this string toSearch, string toFind)
   return new Regex(@"\A" + new Regex(@"\.|\$|\^|\{|\[|\(|\||\)|\*|\+|\?|\\").Replace(toFind, ch => @"\" + ch).Replace('_', '.').Replace("%", ".*") +  @"\z", RegexOptions.Singleline).IsMatch(toSearch);

bool willBeTrue = "abcdefg".Like("abcd_fg");
bool willAlsoBeTrue = "abcdefg".Like("ab%f%");
bool willBeFalse = "abcdefghi".Like("abcd_fg");



or use sql : where CONVERT(VARCHAR(24),yourdate, 103) like '%yourvalue%'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download