Introduction

The Like keyword in VB.Net is not very well known, I just found out today ;-). Well I have probably seen it before but I keep forgetting it is there. But writing about it helps me to remember.

VB.Net

The Like keyword in V.Net works like the Like keyword in SQL. The following passing tests prove this.

```vbnet Imports NUnit.Framework

<TestFixture()> Public Class LikeTests <Test()> Public Sub IfLikeReturnsTrueWhenUsingOneCharacterWildcard() Assert.IsTrue(“st” Like “?t”) End Sub

&lt;Test()&gt;
Public Sub IfLikeReturnsFalseWhenUsingOneCharacterWildcardFindsNothing()
    Assert.IsFalse("test" Like "?t")
End Sub

&lt;Test()&gt;
Public Sub IfLikeReturnsTrueWhenUsingMultiCharacterWildcardBeginswith()
    Assert.IsTrue("test" Like "te*")
End Sub

&lt;Test()&gt;
Public Sub IfLikeReturnsTrueWhenUsingMultiCharacterWildcardEndswith()
    Assert.IsTrue("test" Like "*st")
End Sub

&lt;Test()&gt;
Public Sub IfLikeReturnsTrueWhenUsingMultiCharacterWildcardInBetween()
    Assert.IsTrue("test" Like "*st*")
End Sub

&lt;Test()&gt;
Public Sub IfLikeReturnsFalseWhenUsingMultiCharacterWildcardAndNothingIsFound()
    Assert.IsFalse("test" Like "*a*")
End Sub

End Class``` What the above tell us is that we can use wildcards (? for one character and * for multi character).

You can find everything about Like on MSDN.

C

There is no equivalent of this in C#. 😉

Edit

But you can reference the VisualBasic Assembly if you want. And then write something ugly like this.

```csharp using System; using Microsoft.VisualBasic; using Microsoft.VisualBasic.CompilerServices;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Console.WriteLine(LikeOperator.LikeString(“Test”, “*t”, CompareMethod.Text)); Console.ReadLine(); } } }``` Perhaps there is even a better way to do this.

Conclusion

Something you should now exists because it can be very handy someday. At least you can avoid using regexp for the above cases. So this falls under the category, good to know.