Compare string like file name compare

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pureenhanoi
    New Member
    • Mar 2007
    • 175

    #1

    Compare string like file name compare

    Hi bro!
    Can anyone tell me, how to compare two strings like Operating System compare file names.
    Example: if i have two string "T*.DOC" and "TIET1.DOC" . The comparision operator must return TRUE values when compare them
  • lotus18
    Contributor
    • Nov 2007
    • 865

    #2
    Originally posted by pureenhanoi
    Hi bro!
    Can anyone tell me, how to compare two strings like Operating System compare file names.
    Example: if i have two string "T*.DOC" and "TIET1.DOC" . The comparision operator must return TRUE values when compare them
    How about StrComp? ..

    Comment

    • pureenhanoi
      New Member
      • Mar 2007
      • 175

      #3
      Originally posted by lotus18
      How about StrComp? ..
      StrComp cannot recorgnize special characters like "*", or "?"

      See: StrComp("TIE1.D OC","TIE1.DOC ") -> return 0 (its correct)
      StrComp("TIE*.D OC","TIE1.DOC ") -> return -1 (its correct too)
      but, what it like is StrComp("TIE*.D OC","TIE1.DOC ") will return 0, because "*" character is represent for "1", or any characters in front of ".DOC".

      Thx for your help

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        I'd suggest trying the Like operator.

        Comment

        • pureenhanoi
          New Member
          • Mar 2007
          • 175

          #5
          Originally posted by Killer42
          I'd suggest trying the Like operator.
          Thanks for you help. I tried using LIKE operator. It works verry well with "*" character. But it recorgnize "*" character only, and all character are in case sensitive. With other characters like "?", "0", "9", it cannot work.
          Example: ("T12345.DOC " LIKE "T*.DOC") -> return TRUE (its good)
          ("T12345.doc " LIKE "T*.DOC") -> return False (abit terrible - LIKE are case sensitive operator)
          ("Ta" LIKE "T?") -> return FALSE (terrible)
          ("T123" LIKE "T9") -> return FALSE (cannot recorgnize "9" can be replace character for Numeric)

          I've just written a Function (its abit longer than which it can do :D), but i think it works better than LIKE operator. I show it here to people who need it
          [CODE=vb]
          Public Function FileNameCmp(sNa me1 As String, sName2 As String) As Boolean
          If sName1 = "" And sName2 = "" Then
          FileNameCmp = True
          Exit Function
          End If
          If InStr(sName1, "*") <= 0 And InStr(sName1, "?") <= 0 And InStr(sName1, "9") <= 0 And InStr(sName1, "0") <= 0 Then 'ko con ky tu dai dien
          If InStr(1, sName2, sName1, vbTextCompare) <= 0 Then
          FileNameCmp = False
          Else
          FileNameCmp = True
          End If
          Else 'con ky tu dai dien
          Dim i As Integer
          Dim j As Integer
          i = 1
          j = 1
          If UCase(mId(sName 1, i, 1)) = UCase(mId(sName 2, j, 1)) Then
          FileNameCmp = FileNameCmp(mId (sName1, 2), mId(sName2, 2))
          Exit Function
          Else 'ko bang nhau
          If mId(sName1, i, 1) = "9" Then 'gap chu so, bat buoc phai co
          If IsNumeric(mId(s Name2, j, 1)) Then 'neu chuoi can so sanh cung co chu so o vi tri tuong ung -> so sanh tiep
          'nhay qua cac chu so o chuoi can so sanh
          Do While IsNumeric(mId(s Name2, j, 1))
          j = j + 1
          Loop
          'va nhay qua tat ca cac so 9 o chuoi mau
          Do While mId(sName1, i, 1) = "9"
          i = i + 1
          Loop
          'so sanh tiep phan con lai
          FileNameCmp = FileNameCmp(mId (sName1, i), mId(sName2, j))
          Exit Function
          Else 'neu ko co chu so -> chac chan la sai
          FileNameCmp = False
          Exit Function
          End If
          ElseIf mId(sName1, i, 1) = "0" Then 'gap chu so, ko bat buoc phai co
          'nhay qua cac chu so 0 o chuoi mau
          Do While mId(sName1, i, 1) = "0"
          i = i + 1
          Loop
          'nhay qua cac chu so, neu co o chuoi can so sanh
          Do While IsNumeric(mId(s Name2, j, 1))
          j = j + 1
          Loop
          'so sanh tiep phan con lai
          FileNameCmp = FileNameCmp(mId (sName1, i), mId(sName2, j))
          Exit Function
          ElseIf mId(sName1, i, 1) = "?" Then 'gap dau hoi
          FileNameCmp = FileNameCmp(mId (sName1, i + 1), mId(sName2, j + 1))
          ElseIf mId(sName1, i, 1) = "*" Then 'gap dau *
          'neu day la ky tu cuoi cung -> so sanh dung
          If Len(mId(sName1, i)) = 1 Then
          FileNameCmp = True
          Exit Function
          End If
          'tim ky tu dau tien khac dau * va ? sau dau * vua gap
          Do While mId(sName1, i, 1) = "*" Or mId(sName1, i, 1) = "?"
          i = i + 1
          Loop
          j = InStr(sName2, mId(sName1, i, 1))
          If j = 0 Then 'ko co' ky tu nay -> chac chan sai
          FileNameCmp = False
          Else 'co tim thay
          FileNameCmp = FileNameCmp(mId (sName1, i), mId(sName2, j))
          End If
          Else
          FileNameCmp = False
          End If
          End If
          End If
          End Function
          [/CODE]

          Now, i can use FileNameCmp("T* .DOC","T12345.d oc") -> return TRUE
          FileNameCmp("T9 .DOC","T12345.D OC") -> TRUE - 9 are number, required
          FileNameCmp("T? b.DOC","Tab.DOC ") -> TRUE
          FileNameCmp("T0 abc.DOC","Tabc. DOC") -> TRUE - 0 are number, donot required

          Comment

          • QVeen72
            Recognized Expert Top Contributor
            • Oct 2006
            • 1445

            #6
            Hi,

            I came up with this Function..

            [code=vb]
            Private Function MyCompare(ByVal MainStr As String, ByVal Str1 As String) As Boolean
            MainStr = UCase(MainStr)
            Str1 = UCase(Str1)
            Str1 = Replace(Str1, "0", "*")
            Str1 = Replace(Str1, "1", "*")
            Str1 = Replace(Str1, "2", "*")
            Str1 = Replace(Str1, "3", "*")
            Str1 = Replace(Str1, "4", "*")
            Str1 = Replace(Str1, "5", "*")
            Str1 = Replace(Str1, "6", "*")
            Str1 = Replace(Str1, "7", "*")
            Str1 = Replace(Str1, "8", "*")
            Str1 = Replace(Str1, "9", "*")
            MyCompare = (MainStr Like Str1)
            End Function
            [/code]

            Regards
            Veena

            Comment

            • pureenhanoi
              New Member
              • Mar 2007
              • 175

              #7
              Originally posted by QVeen72
              I came up with this Function ...
              It seems very good. But it may not distinguish Required and Not Required numbers.
              If you've used MS-Access, you can see the Format String. In format string, "0" are not required numbers and "9" are required numbers
              Last edited by Killer42; Mar 12 '08, 02:11 AM. Reason: Reduced quote block, corrected "distinguild" and "verry".

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                The Like operator in combination with Ucase() or Lcase() function will do the wildcard checking alright. If you need to be able to check for things like numeric positions, then you probably need to use regular expressions - not sure whether they are supported in VB.
                Last edited by Killer42; Mar 12 '08, 02:14 AM.

                Comment

                Working...