InStr not returning correct value

When using InStr, if the source string contains a parenthesis or period, InStr returns 0.

For example:

dim source, sFind as string 
dim num as Integer
source = "Phillip (CPA)"
sFind = "cpa"
num = source.InStr(sFind)  //always returns 0

source = "Phillip (CPA)"
sFind = "phi"
num = source.InStr(sFind)  //returns 1

source = "15.38"
sFind = "15.38"
num = source.InStr(sFind)  //also always returns 0

source = "15.38"
sFind = "15"
num = source.InStr(sFind)  //returns 1

Is this the correct behavior of InStr?

Xojo 2018 r3 MacOS

I have just tested your code with Xojo 2021 r1.1 (macOS) and it works correctly. It returned 10, 1, 1, 1.

Now I tested it with Xojo 2018 r3 (macOS) and it worked correctly as well, it returned 10, 1, 1, 1.

Check your code for invisible ascii character that might be throwing off the search, select a block of code, right click then Clean invisible ascii characters. This sometimes happens if you’re copy/pasting from another source.

Did not know about the “clean invisible ascii characters”. It didn’t look like it did anything but funny thing, it seems to be working now.
I don’t remember pasting the code here, but gremlins are sneaky little buggers.
Thanks.

1 Like

OK, so this is interesting. I made a request dialog with a TextArea and a default button. When I hit the button, the string passed is the correct length, but when I hit the Enter key, an additional character is added to the string passed which is why the InStr function was being thrown off.
Who knew?
I guess I have to add a Mask to the TextArea to filter out these characters.

On a side note … InStr is a lot slower than Instr

Really, there are two methods with the same name but a different ortograph ?

The difference is API1 (Instr) and API2 (InStr)
There are other examples and feedback cases for this.

API2 isnt IndexOf ?

2 Likes

Sorry, my bad. That is correct. Need some sleep, I guess. :slight_smile:

But IndexOf is slower than Instr.