InStr(...) gives error?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

InStr(...) gives error?

17 Apr 2024, 11:16

Why this example return 13 instead 16?

Code: Select all

s := "              1234567890123456"
sOriginalString := "qweQWEghjklöpoiqwe123456df"
sSubStr := "QWE"
iTest := Instr( sOriginalString, sSubStr, 0,, 3 )
User avatar
kunkel321
Posts: 1136
Joined: 30 Nov 2015, 21:19

Re: InStr(...) gives error?

17 Apr 2024, 11:20

I'm getting 17...
ste(phen|ve) kunkel
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: InStr(...) gives error?

17 Apr 2024, 11:23

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?
gregster
Posts: 9085
Joined: 30 Sep 2013, 06:48

Re: InStr(...) gives error?

17 Apr 2024, 11:27

AHK v2.0.12:
instr.png
(19.45 KiB) Downloaded 170 times
User avatar
kunkel321
Posts: 1136
Joined: 30 Nov 2015, 21:19

Re: InStr(...) gives error?

17 Apr 2024, 11:30

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)
ste(phen|ve) kunkel
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: InStr(...) gives error?

17 Apr 2024, 11:31

:-(
The same, identical, saved alone in a test file, return me 13... why?
User avatar
kunkel321
Posts: 1136
Joined: 30 Nov 2015, 21:19

Re: InStr(...) gives error?

17 Apr 2024, 11:34

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??
ste(phen|ve) kunkel
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: InStr(...) gives error?

17 Apr 2024, 11:35

I manual copied your and it showed 13.
gregster
Posts: 9085
Joined: 30 Sep 2013, 06:48

Re: InStr(...) gives error?

17 Apr 2024, 11:38

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.
User avatar
kunkel321
Posts: 1136
Joined: 30 Nov 2015, 21:19

Re: InStr(...) gives error?

17 Apr 2024, 11:40

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)
ste(phen|ve) kunkel
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: InStr(...) gives error?

17 Apr 2024, 11:41

How I can encode right the file?
Can you tell me where is recommended the right encode?
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: InStr(...) gives error?

17 Apr 2024, 11:43

Kunkel321: yes it return me 5, encoding it like before.
gregster
Posts: 9085
Joined: 30 Sep 2013, 06:48

Re: InStr(...) gives error?

17 Apr 2024, 11:45

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...
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: InStr(...) gives error?

17 Apr 2024, 11:52

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.
gregster
Posts: 9085
Joined: 30 Sep 2013, 06:48

Re: InStr(...) gives error?

17 Apr 2024, 11:55

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).
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: InStr(...) gives error?

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.
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: InStr(...) gives error?

17 Apr 2024, 12:04

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?
gregster
Posts: 9085
Joined: 30 Sep 2013, 06:48

Re: InStr(...) gives error?

17 Apr 2024, 12:04

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?
User avatar
kunkel321
Posts: 1136
Joined: 30 Nov 2015, 21:19

Re: InStr(...) gives error?

17 Apr 2024, 12:12

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.
ste(phen|ve) kunkel
Archimede
Posts: 543
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: InStr(...) gives error?

17 Apr 2024, 12:13

:-(
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...

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: akirofe, mikeyww, onandoffwhat, songdg, WarlordAkamu67 and 23 guests