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 2699
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.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
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.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
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |