473,763 Members | 5,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare string like file name compare

pureenhanoi
175 New Member
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 2714
lotus18
866 Contributor
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 New Member
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
Mar 1 '08 #3
Killer42
8,435 Recognized Expert Expert
I'd suggest trying the Like operator.
Mar 3 '08 #4
pureenhanoi
175 New Member
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.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
Mar 7 '08 #5
QVeen72
1,445 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Expert
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
10879
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 new database. For instance, her old database has a table with Band Info in it. Her new database also has a table with Band Info in it but slightly different. I was wondering if there was an easy way to compare the fields from similar tables in...
19
9519
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 have tried to print out by debug.writeline(). But the "==" operator results false, and string.Compare() results true. Somebody helps me!
2
2488
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 String. else I check if RightExpression is a int. else In final case, I check that RightExpression is a name of object
3
4945
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 which hold some value that can only be look up after i compare both string so i can find where to look up the value of object type 217 and the instance number 570
4
6662
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
3689
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 strFileName with strFileName1.if there is any mistek or some word is missing in second string then that words should be highlight(in sense change the color of that words) by the button click event. pls somebody give an idea to me.
7
3138
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 std::ifstream; using std::ofstream; #include <stdio.h> #include <cstdlib> #include <string>
6
1573
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 with other animal for example compare a Chicken with a Cow in some way or a Cow with another Cow
4
3022
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 thanks e.g i have file with name data.txt with file like this 123456
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.