Page 1 of 2

InStr(...) gives error?

Posted: 17 Apr 2024, 11:16
by Archimede
Why this example return 13 instead 16?

Code: Select all

s := "              1234567890123456"
sOriginalString := "qweQWEghjklöpoiqwe123456df"
sSubStr := "QWE"
iTest := Instr( sOriginalString, sSubStr, 0,, 3 )

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:20
by kunkel321
I'm getting 17...

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:23
by Archimede
I made the copy-paste of my test program...
I made the copy-paste from here to my AutoHotkey and it return 13...
17 is not right, but is no 13... ( that is no right too ) why?

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:27
by gregster
AHK v2.0.12:
instr.png
(19.45 KiB) Downloaded 170 times

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:30
by kunkel321
I think it might make a difference if your ahk file is not saved as format "UDF-8 with BOM." With the below, I get 15. I think the accented o character might get converted to two characters(?)

Code: Select all

#SingleInstance
#Requires AutoHotkey v2.0

sOriginalString := "qweQWEghjklpoiqwe123456df"
sSubStr := "QWE"
msgbox iTest := Instr(sOriginalString, sSubStr, 0,, 3)

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:31
by Archimede
:-(
The same, identical, saved alone in a test file, return me 13... why?

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:34
by kunkel321
Archimede wrote:
17 Apr 2024, 11:31
:-(
The same, identical, saved alone in a test file, return me 13... why?
Did you try mine and it showed 13??

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:35
by Archimede
I manual copied your and it showed 13.

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:38
by gregster
Archimede wrote:
17 Apr 2024, 11:35
I manual copied your and it showed 13.
You are obviously using ANSI encoding for your script file and not the recommended UTF-8 (with or without BOM). I was using UTF-8 with BOM in my test above.
When I try ANSI, I get 13... with UTF-8 and UTF-8 with BOM, I get 16.

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:40
by kunkel321
Weird... Sorry, I don't know why.

Does this give you '5' ?

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+

sOriginalString := "123456789"
sSubStr := "5"
msgbox iTest := Instr(sOriginalString, sSubStr, 0,,)
Also, sorry, I put "UDF-8 with BOM" but I meant "UTF-8 with BOM." (unicode text format)

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:41
by Archimede
How I can encode right the file?
Can you tell me where is recommended the right encode?

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:43
by Archimede
Kunkel321: yes it return me 5, encoding it like before.

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:45
by gregster
I already linked to it above: https://www.autohotkey.com/docs/v2/FAQ.htm#nonascii

in Notepad - Save as:

notepad.png
(9.01 KiB) Downloaded 137 times
In other editors, look for something similar...

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:52
by Archimede
Yes I found it on my editor SciTE4AutoHotkey.
I think you have reason: I substituted "ö " character with "o" and it works...
I set the UTF-8 encode, with the "ö" character and it seem to work.
It seem to work with UTF-8 with BOM encode, too.
I think this problem must be explained well on the help manual, usage and syntax.

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 11:55
by gregster
It is in the FAQ 🤷‍♂️ - and before it has been for ages in the AHK v1 FAQ (then, only UTF-8 with BOM was recommended; v2 doesn't necessarily need the BOM).

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 12:00
by Archimede
I used the v1 some years without seeing this problem... I think this is a very important thing, then is important to explain it on the hel manual, usage and syntax.

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 12:04
by Archimede
Now I have to convert to UTF-8 my big library.
Editing it with the UTF-8 encode, shows all characters "è" like "xE9": how I can automatically substitute all chars?

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 12:04
by gregster
Archimede wrote:
17 Apr 2024, 12:00
I used the v1 some years without seeing this problem... I think this is a very important thing, then is important to explain it on the hel manual, usage and syntax.
There are plenty of topics on the v1 forums about this. But if people don't read the docs - and the FAQs are an important part of the introduction - it can't be helped.
how I can automatically substitute all chars?
The Replace function of the editor or write an AHK script to do it?

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 12:12
by kunkel321
Archimede wrote:
17 Apr 2024, 12:04
Editing it with the UTF-8 encode, shows all characters "è" like "xE9": how I can automatically substitute all chars?
My autocorrect tool has a bunch of accented-words. Something I remember running in to, is that, if your characters are already messed up, then changing the formatting to UTF won't fix them. Instead, I had to create a new ahk file with the correct encoding, then paste the code into it after creating it.

(Not sure if this will apply to your situation though.)

Of course, be sure to make a backup copy before you try any conversions.

Re: InStr(...) gives error?

Posted: 17 Apr 2024, 12:13
by Archimede
:-(
The replace function no found the "xE9" character...
I read the manual everytime, but the FAQ no everytime, only when it seem useful...
I think this thing is very importat to write it on the standard manual, not on the FAQ.... but it is ONLY a my opinion...