Ever wanted to implement wildcard functionality witin your programs? Just as you’re used to in the good old command prompt.
E.g: h*nri? B*ch
Going to implement such, you’ll probably turn to regular expressions as your way to solve the problem (updated: see this post on how to do that). But if you’re not that familiar with the regular expression syntax, you could implement such in another way.
if you’re a VB.NET developer you probably already know that VB.NET offers a function called LikeString(). This function that accepts a search string and pattern, enabling you to do such Wildcard matching. There’s nothing wrong in calling this function from C#. The function resides in the Microsoft.VisualBasic assembly, so you need to add a reference to that.
The Operators class is a VB.NET class where the LikeString resides, so to use it you can simply use the following syntax:
Operators.LikeString(input, pattern, compMethod);
where:
input: is the string to examine
pattern: the string containing the pattern to try matching with
compMethod: specifies how the operation is to be performed (CompareMethod.Text or CompareMethod.Binary)
using Microsoft.VisualBasic; using Microsoft.VisualBasic.CompilerServices; if(Operators.LikeString("This is just a test", "*just*", CompareMethod.Text)) { Console.WriteLine("This matched!"); }
The two aliases is because the CompareMethod enum is part of one namespace and Operators class the other namespace.