Visual Basic has a useful Operator, the “Like” Operator. Observe it in action:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Option Explicit On Module Module1 Sub Main() Dim TestString As String() = New String() {"Turkey", "Tosser", "Jabbawocky", "Ham", "Hamster", "Harp"} Dim TestMask As String = "H*" For Each Iterate As String In TestString If Iterate Like TestMask Then Console.WriteLine(Iterate & " Is Like " + TestMask) End If Next Console.ReadKey() End Sub End Module |
The Like operator is a pattern matching operator. It can be useful in many situations and is certainly a lot shorter to use than fiddling about with Regular Expression Objects, matches, etc. So the question becomes, how can we best bring this to C#? We could have a static method in a separate class:
1 2 3 4 5 |
public static bool IsLike(String value, String mask, RegexOptions options = RegexOptions.Multiline&RegexOptions.IgnorePatternWhitespace) { String usepattern = "^" + Regex.Escape(mask).Replace("\\*", ".*").Replace("\\?", ".") + "$"; return Regex.IsMatch(value, usepattern, options); } |
And use it like this:
1 |
if(Like.IsLike(Iterate,TestMask)) |
But that’s not very concise. One alternative is to cheat and use operator overloads; we can’t define a new operator full stop, but we can fake it by creating a new class and a explicit operator for it that converts a string. Resulting in something like this:
1 |
Console.WriteLine(Iterate == (Like)TestMask); |
In order to do this, we need as mentioned a new class that has an explicit cast from a String as well as an equality operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class Like { private String sPattern; public String Pattern { get { return sPattern; } private set { sPattern = value; } } private RegexOptions _Options; public RegexOptions Options { get { return _Options; } private set { _Options = value; } } public Like(String sPattern,RegexOptions pOptions=RegexOptions.Multiline) { Pattern=sPattern; Options = pOptions; } public bool Matches(String test) { if (test == null) throw new ArgumentNullException("test"); return IsLike(test, Pattern, Options); } public static explicit operator Like(String Source) { return new Like(Source); } public static bool operator ==(String first,Like second){ if (second == null) throw new ArgumentNullException("second"); return second.Matches(first); } public static bool operator !=(string First, Like second) { return !(First == second); } private static bool IsLike(String value, String mask, RegexOptions options = RegexOptions.Multiline&RegexOptions.IgnorePatternWhitespace) { String usepattern = "^" + Regex.Escape(mask).Replace("\\*", ".*").Replace("\\?", ".") + "$"; return Regex.IsMatch(value, usepattern, options); } } |
And… That’s it! Now you can have something approximating the Like Operator. Note that because it uses Regular Expressions it’s not actually the Like Operator, which is more restricted. If you really need exact replication of VB.NET’s Like Operator, you can create an Assembly in VB.NET and expose a public function that simply returns the result of a Like Operator on it’s parameters.
Have something to say about this post? Comment!