Discussion:
String comparisons in VB6
(too old to reply)
KAB
2006-02-04 18:04:39 UTC
Permalink
Hi Sean:
InStr should have worked (I've used it many times). Be sure the strings
have matched cases (upper case/ lower case). Also, note that InStr has four
different comparing methods, so you may wish to state "1" in the function to
force a text comparison.

string1=InStr(1,string1,"DRA-SYMB",1)
or
string1=InStr(3,string1,"DRA-SYMB",1)

To truncate the E- use the "Right String"
or "Mid String". For some reason, I found the "Mid" function to be more
reliable than "Left" or "Right" functions so I recommend it first.

string1=Right(string1,len(string1)-2)
or
string1=Mid(string1,3,len(string1)-2)

I hope this helps:
KAB
I have been racking my brain tryign to understand how to compare two string
in vb6. Can someone help?
I have a string E-DRA-SYMB that has to be compared to DRA-SYMB. As long as
string2 is found in string one, I want a return value of true. I tried the
InStr and Like commands but I never get the correct response. What can I do?
How do I truncate the E- from string1 before comparing both strings?
Any help would be greatly appreciated.
Sean Campbell
Rick Rothstein [MVP - Visual Basic]
2006-02-04 18:36:00 UTC
Permalink
Post by KAB
string1=Right(string1,len(string1)-2)
or
string1=Mid(string1,3,len(string1)-2)
When you use the Mid function to get the right-hand portion of a text
string, you don't need to specify the 3rd argument... the default is to get
the remainder of the text string. So, this is equivalent to the above...

string1 = Mid(string1, 3)

Rick
Sean Campbell
2006-02-05 19:37:26 UTC
Permalink
Thanks to you and KAB for the tips. I will try them out.

Here a small follow-up thought: could I write a .NET function (dll) that
uses the EndsWith function and then reference it in VB6 to benefit from the
.NET enhancements to VB?

Sean
Post by Rick Rothstein [MVP - Visual Basic]
Post by KAB
string1=Right(string1,len(string1)-2)
or
string1=Mid(string1,3,len(string1)-2)
When you use the Mid function to get the right-hand portion of a text
string, you don't need to specify the 3rd argument... the default is to get
the remainder of the text string. So, this is equivalent to the above...
string1 = Mid(string1, 3)
Rick
Ken Halter
2006-02-05 21:44:15 UTC
Permalink
Post by Sean Campbell
Thanks to you and KAB for the tips. I will try them out.
Here a small follow-up thought: could I write a .NET function (dll) that
uses the EndsWith function and then reference it in VB6 to benefit from the
.NET enhancements to VB?
Sean
Or, write your own EndsWith function in VB6 in a minute or two and save the
hassle of instantly sucking the performance life out of your app and adding
20+ megabytes to your setup package size.
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm
Bob O`Bob
2006-02-06 00:21:39 UTC
Permalink
Post by Ken Halter
Post by Sean Campbell
Thanks to you and KAB for the tips. I will try them out.
Here a small follow-up thought: could I write a .NET function (dll) that
uses the EndsWith function and then reference it in VB6 to benefit from the
.NET enhancements to VB?
Sean
Or, write your own EndsWith function in VB6 in a minute or two and save the
hassle of instantly sucking the performance life out of your app and adding
20+ megabytes to your setup package size.
I wouldn't know that dotnet function if it bit me (which AAMOF I'm sure it would)

Yet I'd hope something like this would get pretty close:

Public Function EndsWith(StringCheck As String, StringMatch As String, _
Optional Compare As VbCompareMethod = vbBinaryCompare)

EndsWith = (Len(StringCheck) - Len(StringMatch)) _
= InStrRev(StringCheck, StringMatch, , Compare) - 1
End Function




Bob
Larry Serflaten
2006-02-06 02:27:10 UTC
Permalink
Or, for VB5 users:

Function EndsWith(Source As String, _
Match As String, _
Optional Compare As VBA.VbCompareMethod = vbTextCompare) _
As Boolean

EndsWith = Not CBool( _
StrComp( _
Right$(Source, Len(Match)), _
Match, _
Compare _
) _
)
End Function


LFS

Continue reading on narkive:
Loading...