-1

I'm a new developer in C# and I'm migrating code from VB.NET to C#. But the LIKE operator is not working in C#, is there another one with the same function?

The VB.NET code I need to convert looks like this::

If (item Like "[A-D]") Then End If If (item Like "[E-H]") Then End If If (item Like "[I-M]") Then End If If (item Like "[N-V]") Then End If 
4

1 Answer 1

0

LIKE operator does not exist for C#, the solution is to use regular expression.

try this:

if (Regex.IsMatch(item, "^[a-dA-D]+$")) { } if (Regex.IsMatch(item, "^[e-hE-H]+$")) { } if (Regex.IsMatch(item, "^[i-mI-M]+$")) { } if (Regex.IsMatch(item, "^[n-vN-V]+$")) { } 
Sign up to request clarification or add additional context in comments.

1 Comment

If you use RegexOptions.IgnoreCase then you can simplfy those patterns to just "^[A-D]$" - note that VB.NET's Like operator uses [A-B] to denote a single character, so using the one-or-more quantifier (+) will result in different behaviour compared to Like ("AD" won't match Like "[A-D]" but will match Regex.IsMatch( item, "^[A-D]+$" ).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.