How the StrLikeText() method compares the strings which has '#' in C#?

Viewed 46

The Microsoft.VisualBasic.CompilerServices.StringType.StrLikeText() method returns true for the below case,

 Microsoft.VisualBasic.CompilerServices.StringType.StrLikeText("HAL_MAC#207", "hal_mac[#]207")

But returns false for the below case,

     Microsoft.VisualBasic.CompilerServices.StringType.StrLikeText("HAL_MAC#207", "hal_mac#207")

Can anyone let me know how the comparison of the string is calculated?

Is it it mandatory to add the # with [] brackets?

Thanks in advance.

2 Answers

As always, have a look at the documentation first.

The docs say that the second parameter of StrLikeText() is

Any String expression conforming to the pattern-matching conventions described in Like Operator.*

Now lets have a look at the docs of the Like Operator which state

# - Any single digit (0–9)

Therefore you need to escape # when you actually want to match against # otherwise it's recognized as a pattern. This is also stated in the docs by

A group of one or more characters (charlist) enclosed in brackets ([ ]) can be used to match any single character...

yes, you need to add the [] for comparing the charecter '#'. Since its considered as the as a speacial charecter for comparing the values like, StrLikeText("HAL_MAC9", "hal_mac#")

Related