473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Compare string like file name compare

pureenhanoi
175 100+
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
Mar 1 '08 #1
7 2699
lotus18
866 512MB
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? ..
Mar 1 '08 #2
pureenhanoi
175 100+
How about StrComp? ..
StrComp cannot recorgnize special characters like "*", or "?"

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

Thx for your help
Mar 1 '08 #3
Killer42
8,435 Expert 8TB
I'd suggest trying the Like operator.
Mar 3 '08 #4
pureenhanoi
175 100+
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
Expand|Select|Wrap|Line Numbers
  1. Public Function FileNameCmp(sName1 As String, sName2 As String) As Boolean
  2. If sName1 = "" And sName2 = "" Then
  3.     FileNameCmp = True
  4.     Exit Function
  5. End If
  6. 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
  7.     If InStr(1, sName2, sName1, vbTextCompare) <= 0 Then
  8.         FileNameCmp = False
  9.     Else
  10.         FileNameCmp = True
  11.     End If
  12. Else            'con ky tu dai dien
  13.     Dim i As Integer
  14.     Dim j As Integer
  15.     i = 1
  16.     j = 1
  17.     If UCase(mId(sName1, i, 1)) = UCase(mId(sName2, j, 1)) Then
  18.         FileNameCmp = FileNameCmp(mId(sName1, 2), mId(sName2, 2))
  19.         Exit Function
  20.     Else            'ko bang nhau
  21.         If mId(sName1, i, 1) = "9" Then         'gap chu so, bat buoc phai co
  22.             If IsNumeric(mId(sName2, j, 1)) Then        'neu chuoi can so sanh cung co chu so o vi tri tuong ung -> so sanh tiep
  23.                 'nhay qua cac chu so o chuoi can so sanh
  24.                 Do While IsNumeric(mId(sName2, j, 1))
  25.                     j = j + 1
  26.                 Loop
  27.                 'va nhay qua tat ca cac so 9 o chuoi mau
  28.                 Do While mId(sName1, i, 1) = "9"
  29.                     i = i + 1
  30.                 Loop
  31.                 'so sanh tiep phan con lai
  32.                 FileNameCmp = FileNameCmp(mId(sName1, i), mId(sName2, j))
  33.                 Exit Function
  34.             Else            'neu ko co chu so -> chac chan la sai
  35.                 FileNameCmp = False
  36.                 Exit Function
  37.             End If
  38.         ElseIf mId(sName1, i, 1) = "0" Then     'gap chu so, ko bat buoc phai co
  39.             'nhay qua cac chu so 0 o chuoi mau
  40.             Do While mId(sName1, i, 1) = "0"
  41.                 i = i + 1
  42.             Loop
  43.             'nhay qua cac chu so, neu co o chuoi can so sanh
  44.             Do While IsNumeric(mId(sName2, j, 1))
  45.                 j = j + 1
  46.             Loop
  47.             'so sanh tiep phan con lai
  48.             FileNameCmp = FileNameCmp(mId(sName1, i), mId(sName2, j))
  49.             Exit Function
  50.         ElseIf mId(sName1, i, 1) = "?" Then     'gap dau hoi
  51.             FileNameCmp = FileNameCmp(mId(sName1, i + 1), mId(sName2, j + 1))
  52.         ElseIf mId(sName1, i, 1) = "*" Then     'gap dau *
  53.             'neu day la ky tu cuoi cung -> so sanh dung
  54.             If Len(mId(sName1, i)) = 1 Then
  55.                 FileNameCmp = True
  56.                 Exit Function
  57.             End If
  58.             'tim ky tu dau tien khac dau * va ? sau dau * vua gap
  59.             Do While mId(sName1, i, 1) = "*" Or mId(sName1, i, 1) = "?"
  60.                 i = i + 1
  61.             Loop
  62.             j = InStr(sName2, mId(sName1, i, 1))
  63.             If j = 0 Then       'ko co' ky tu nay -> chac chan sai
  64.                 FileNameCmp = False
  65.             Else        'co tim thay
  66.                 FileNameCmp = FileNameCmp(mId(sName1, i), mId(sName2, j))
  67.             End If
  68.         Else
  69.             FileNameCmp = False
  70.         End If
  71.     End If
  72. End If
  73. End Function
  74.  
Now, i can use FileNameCmp("T*.DOC","T12345.doc") -> return TRUE
FileNameCmp("T9.DOC","T12345.DOC") -> TRUE - 9 are number, required
FileNameCmp("T?b.DOC","Tab.DOC") -> TRUE
FileNameCmp("T0abc.DOC","Tabc.DOC") -> TRUE - 0 are number, donot required
Mar 7 '08 #5
QVeen72
1,445 Expert 1GB
Hi,

I came up with this Function..

Expand|Select|Wrap|Line Numbers
  1. Private Function MyCompare(ByVal MainStr As String, ByVal Str1 As String) As Boolean
  2.     MainStr = UCase(MainStr)
  3.     Str1 = UCase(Str1)
  4.     Str1 = Replace(Str1, "0", "*")
  5.     Str1 = Replace(Str1, "1", "*")
  6.     Str1 = Replace(Str1, "2", "*")
  7.     Str1 = Replace(Str1, "3", "*")
  8.     Str1 = Replace(Str1, "4", "*")
  9.     Str1 = Replace(Str1, "5", "*")
  10.     Str1 = Replace(Str1, "6", "*")
  11.     Str1 = Replace(Str1, "7", "*")
  12.     Str1 = Replace(Str1, "8", "*")
  13.     Str1 = Replace(Str1, "9", "*")
  14.     MyCompare = (MainStr Like Str1)
  15. End Function
  16.  
Regards
Veena
Mar 7 '08 #6
pureenhanoi
175 100+
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
Mar 11 '08 #7
Killer42
8,435 Expert 8TB
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.
Mar 12 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
2
by: Locia | last post by:
How can I compare "if argument"? example: if (leftExpression==RightExpression) After parsing I know the type of RightExpression. I suppone that if RightExpression is wrap into " " is a...
3
by: dotnetnoob | last post by:
i have two strings from xml file str1 = 800.7415_801.101_8.115_216.12 str2 = 800.7415_801.101_8.115_216.12_217.570 the first stream represent a xml file 800.7415_801.101_8.115_261.12.xml...
4
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
3
by: Twinkle | last post by:
HI there i want to compare between two strings char by char.every strings having a word document.first string name is strFileName and second string name is strFilename1. i want to compare...
7
stealwings
by: stealwings | last post by:
I have a little problem with my program, or maybe it is not that little, anyway here is the code: #include "stdafx.h" #include <iostream> using namespace std; #include <fstream> using...
6
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. Now If I want to implement functionallity so I can compare animal with each other or...
4
by: ndoe | last post by:
how to compare string in file i mean compare that string content digit or alphabet,and 1 question more can we count that line if first line variable = 1,if read second line variable = 2 and so on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.