Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VB.NET Compare two string for full or partial match

How do you programmatically Compare two string for full or partial match.

I may have a string called "ItemName" like "003.00.112.0" and a string I am trying to compare it to the string named "ItemNameToFind" like "001...**.*" where " * " is meant to be an unknown blank spot.

What I have figured out so far is that it’s some sort of an if statement that needs to correctly compare the strings.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If ItemName = ItemNameToFind Then
    MsgBox("Item " & ItemName & " was found based on " & ItemNameToFind)
End If

In the example above it would return that it’s not a match because of the first three symbols in the string.

Could, someone, please, help with the code to make that happen correctly as I explained?

>Solution :

In VB.NET you can use the Like operator:

 Dim ItemName = "001.00.112.0"
 Dim ItemNameToFind = "001.??.???.?"

 If ItemName Like ItemNameToFind Then
     Console.Write("Item " & ItemName & " was found based on " & ItemNameToFind)
 Else
   Console.Write("not found")
End If

Note that i have replaced your * with ? since that means "Any single character".

?   Any single character
*   Zero or more characters
#   Any single digit (0–9)
[charlist]  Any single character in charlist
[!charlist] Any single character not in charlist
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading