Eduards
Eduards

Reputation: 68

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.

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?

Upvotes: 1

Views: 1511

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460098

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

Upvotes: 2

Related Questions