Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare two strings 3

Status
Not open for further replies.

Palmyra

Programmer
Jan 5, 2007
150
US
I'm trying to compare names. I have two strings. String1 will have one name, e.g., 'JohnSmith'. String2 may have more than one name, e.g., 'John Smith; Mary Smith; Joan Smith'.

I need to be able to tell if John Smith in String1 is anywhere in String2.

Any help appreciated.

 
strongm, I sort of agree, yet sort of not. InStr is not Boolean. The value is 0 (or not). We interpret - as you state, from one perspective - that 0 as False.
Code:
Dim strIn As String
Dim bolWhatever As Boolean
strIn = "Badda bing, badda boom"

MsgBox InStr(1, strIn, "yadda") & vbCrLf _
   & bolWhatever
will display:

0
False

On the other hand, you are absolutely correct in stating that "true", is anything other than 0.
Code:
Dim bolWhatever As Boolean
Dim j As Long
j = 2317826
bolWhatever = j
Msgbox "bolWhatever set as " & j & vbCrLf _
   & bolWhatever
will display:

bolWhatever set as 2317826
True

HOWEVER, the only way to get the number is to use a number variable (in this case, j), and display THAT.
Code:
Dim bolWhatever As Boolean
bolWhatever = 2317826
As a boolean, you can give it a number - 2317826 for example - but you can never get that number. bolWhatever does not equal 2317826 (even with the statement bolWhatever = 2317826). It = True. Or as you put it correctly "Not 0".
Code:
bolWhatever = 2317826
If bolWhatever = 2317826 Then
   MsgBox "Worked."
Else
   MsgBox "Not worked"
End If
will always return "Not worked", no matter what number you make bolWhatever equal. You can give a true boolean variable any number you want, but it will never keep it. bolWhatever does not really = 2317826, even if there is an explicit instruction. It = Not 0, or True. You can not even get a 0 value from a true boolean. You get "False". You can use 0 in a logic statement:

If bolWhatever = 0

but you can never GET a 0 from a boolean.

bolWhatever = 0
Debug.Print bolWhatever

will not display 0, it displays False.

InStr though is a Long, not a Boolean. So yes, we humans interpret Instr(0) as False, but it really means .....0

The point being is that 0 does not really mean "False".

It could mean THREE possible things:

string1 is zero-length
string2 is not found (your False)
start > string2

In other words, suppose you accidentally gave a Start greater than that a TRUE found.....

strWhatever = "This is a big sentence."

InStr(13, strWhatever, "big")

will return a 0. Even though, it is TRUE, that "big" really is in the given string.

So yes we interpret InStr(0) as "False", and it probably does mean that, it does not really mean False. It means....0

faq219-2884

Gerry
My paintings and sculpture
 
PHV and rmikesmith

Thank you. I guess staring for 10 minutes doesn't always mean reading carefully.
 
>As a boolean, you can give it a number - 2317826 for example - but you can never get that number.

At what point have I suggested otherwise?

>You can give a true boolean variable any number you want, but it will never keep it.

It will maintain its True or False value, which is all that we require from a Boolean.

>but you can never GET a 0 from a boolean.
>bolWhatever = 0
>Debug.Print bolWhatever

Debug.Print 5 + bolWhatever

We just can't see it directly because of some display decisions made by Microsoft for VB/VBA

>It could mean THREE possible things:

But correctly only two (boolean) results

>In other words, suppose you accidentally gave a Start greater than that a TRUE found.....

So, you are asking whether "big" is in " sentence.". Which is clearly false. Why would you expect otherwise? Your argument here is, I'm afraid, silly






 
I don't see Gerry's argument as silly, I see his point directly related to the suggestion you gave regarding a boolean response. As I've always stated, just because we can do something, doesn't mean we should do it that way. There is definitely something to be said about good practices. :)

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
My comment about a silly argument is related to Gerry's assertion that

'Even though, it is TRUE, that "big" really is in the given string'

because it isn't true (or True). The string that is being searched is " sentence.", not "This is a big sentence."

>There is definitely something to be said about good practices

Sorry, are you suggesting that using the fact that one can interpret 0 as False and non-0 as True (which is basically all I've been saying) is bad practice?
 
Uh, yeah, pretty sure that is what I just said. Can it be done, sure. Should it be, no. Just the same as Option Explicit should be used. Do we have to, no. Should we, yes. MHO.

I understood what Gerry said.

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top