QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: hanness on April 10, 2021, 06:13:45 pm
-
I'm sure that I'm missing something really simple here, but I'm a little bit confused about how I can get some Boolean results out of something like an INSTR.
Here is an example of what I am trying to do...
Suppose I have a string that I set like this:
a$ = "hello world"
Now I run this code:
If InStr(a$, "world") Then
Print "The word world was present in the string"
End If
In that case the message will be printed because InStr evaluates to TRUE since "world" is present in the string.
Now, suppose I want to take an action if "WORLD" is in the string AND "hello" is NOT in the string.
Here is how I tried to structure this:
If (InStr(a$, "world")) AND (NOT (InStr(a$,"hello"))) Then
Print "The word world was present and hello was not"
End If
I would expect that to fail because both "hello" and "world" are present and yet the message is still displayed.
I realize that I could perform 2 seperate operations, 1 to check for the presence of "world" and another to check for "hello" but I wanted to see if this could be one on a single line.
I realize that INSTR does not return a simple TRUE or FALSE, rather, it returns the position the string sought, which would be 0 if not present, but isn't there some simple way to convert that into a simple TRUE or FALSE?
Any thoughts on this?
-
LOL. Been looking for quite a while, 3 minutes after my post I figured it out.
My apologies, please ignore.
-
If (InStr(a$, "world")) <> 0 AND (NOT (InStr(a$,"hello"))) = -1 Then....
Will the above work?
-
If InStr(a$, "world") > 0 AND InStr(a$,"hello") = 0 Then
Print "The word world was present and hello was not"
End If
-
Also notice that InStr is case sensitive.
-
IF INSTR(LCASE$(a$), "world") AND INSTR(LCASE$(a$), "hello") = 0 THEN
' do whatever
END IF
Now it's insensitive, like me.
Pete
-
a$="hello world and every body"
'With a variable (here ok), you can create complex conditions.
ok=0
'here 4 tests with 'word", with "hello", without 'universe', len(a$)<200...
if instr(ucase$(a$),"HELLO")>0 then ok=ok+1
if instr(ucase$(a$),"WORD")>0 then ok=ok+1
if instr(ucase$(a$),"UNIVERSE")=0 then ok=ok+1
if len(a$)<200 then ok=ok+1
and so on...
Now'
if ok=4 then print "yes" else print" no"
sleep
-
IF INSTR(LCASE$(a$), "world") AND INSTR(LCASE$(a$), "hello") = 0 THEN
' do whatever
END IF
Now it's insensitive, like me.
Pete
Pete’s joke aside, @hanness notice InStr is case sensitive and will not find "World" in "Hello, world!".
-
You can make both arguments be all capitals with UCase$() if you want to make search case-insensitive:
If InStr(UCase$(a$), UCase$("world")) > 0 AND InStr(UCase$(a$), UCase$("hello")) = 0 Then
Print "The word world was present and hello was not"
End If
-
You can make both arguments be all capitals with UCase$() if you want to make search case-insensitive:
If InStr(UCase$(a$), UCase$("world")) > 0 AND InStr(UCase$(a$), UCase$("hello")) = 0 Then
Print "The word world was present and hello was not"
End If
What’s wrong with Pete’s method for insensitive comparison? Seems to me that it’d work fine without the extra overhead of that second UCASE$ needed.
IF INSTR(LCASE$(a$), "world") <> 0 AND INSTR(LCASE$(a$), "hello") = 0 THEN
' do whatever
END I
-
What’s wrong with Pete’s method for insensitive comparison? Seems to me that it’d work fine without the extra overhead of that second UCASE$ needed.
IF INSTR(LCASE$(a$), "world") <> 0 AND INSTR(LCASE$(a$), "hello") = 0 THEN
' do whatever
END I
Nothing wrong, I just thought it was a joke and that he was pointing out the difference between INSTR and InStr, when I failed to see the LCASE in there. My bad.
-
LOL - I parsed out laughing. :D
Pete
-
Thanks, everyone. I saw my logic error minutes after I posted the original question even though I had been at it for several hours.
You ever just keep looking at something with the same mindset and just not seeing something right there in front of you?
What's funny is that the little time break where I composed the question and posted it here, followed by a quick little break to get a drink allowed me to look at it just slightly differently when I came back and that was all I needed.
-
Yep, taking a break or explaining problem to someone else in words often helps :)
And sometimes you just keep missing something simple and sometimes you just don't know.
-
A good stiff drink often helps change my mindset too.