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
7 2714
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? ..
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
I'd suggest trying the Like operator.
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 -
Public Function FileNameCmp(sName1 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(sName1, i, 1)) = UCase(mId(sName2, 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(sName2, 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(sName2, 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(sName2, 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
-
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
QVeen72 1,445
Recognized Expert Top Contributor
Hi,
I came up with this Function.. -
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
-
Regards
Veena
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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!
|
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
|
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
|
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
| |
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.
|
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>
|
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
|
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
|
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,...
|
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...
| |
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |