VB.net compares a complete string with one string containing wildcards

I have a method that takes 2 string arguments. One that contains a normal line and one line containing a line with one or more wildcard characters. I tried the following code:

Private Function DoesMatchWildcardString(ByVal fullString As String, ByVal wildcardString As String) As Boolean
    Dim stringParts() As String
    Dim matches As Boolean = True

    stringParts = wildcardString.Split("*")

    For Each str As String In stringParts
        If fullString.Contains(str) = False Then
            matches = False
        End If
    Next

    Return matches

End Function

I realized that it will not work properly. If I have ABCD as normal and A * CD as my wildcard, the match will work even if my normal string was CDAB, which is not what I want.

Any ideas?

Many thanks.

+3
source share
3 answers

, . shift-or algorithm ( , "bitap" , ).

: *, , .

, Contains IndexOf , , . , , . . , , shift-or .

, VB : Like

If fullString Like wildcardString Then
    ' Yep, matches.
End If

:

, .

Dim stringParts As String() = wildcardString.Split("*")
' or, with Option Infer On:
Dim stringParts = wildcardString.Split("*")

, (If X = False...).

If fullString.Contains(str) Then
+7

"*" char, . , , if maskedtextbox1.text like maskedtextbox2.text maskedtextbox2.text = maskedtextbox1.text .

0

, ?

Private Function DoesMatchWildcardString(ByVal fullString As String, ByVal wildcardString As String) As Boolean
    Dim count As Integer = 1
    Dim wildchr As String
    Dim fschr As String
    Dim resultstring As String = String.Empty

    Do Until count - 1 = Len(wildcardString)
        wildchr = Mid$(wildcardString, count, 1)
        fschr = Mid$(fullString, count, 1)
        If wildchr = "*" Then ' this one matches any char
            resultstring = resultstring & fschr
        Else
            If wildchr = fschr Then
                resultstring = resultstring & fschr
            End If
        End If
        count = count + 1
    Loop
    Return resultstring = fullString
End Function
0
source

All Articles