Home All Groups Group Topic Archive Search About

String comparisons in VB6

Author
4 Feb 2006 6:08 PM
Sean Campbell
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

Author
4 Feb 2006 6:04 PM
KAB
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

Show quoteHide quote
"Sean Campbell" wrote:

> 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
Author
4 Feb 2006 6:36 PM
Rick Rothstein [MVP - Visual Basic]
> 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
Author
5 Feb 2006 7:37 PM
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

Show quoteHide quote
"Rick Rothstein [MVP - Visual Basic]" wrote:

> > 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
>
>
>
Author
5 Feb 2006 9:44 PM
Ken Halter
"Sean Campbell" <SeanCampb***@discussions.microsoft.com> wrote in message
news:65F37F98-602E-4196-8CE2-B81381AD9C44@microsoft.com...
> 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
Author
6 Feb 2006 12:21 AM
Bob O`Bob
Ken Halter wrote:
Show quoteHide quote
> "Sean Campbell" <SeanCampb***@discussions.microsoft.com> wrote in message
> news:65F37F98-602E-4196-8CE2-B81381AD9C44@microsoft.com...
>> 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
Author
6 Feb 2006 2:27 AM
Larry Serflaten
"Bob O`Bob" <filter***@yahoogroups.com> wrote

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

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