When I tried to search a string in Clipboard, InStr() always returned 0

Get help with using AutoHotkey and its commands and hotkeys
kwfine
Posts: 16
Joined: 26 Nov 2015, 06:46

When I tried to search a string in Clipboard, InStr() always returned 0

26 Jul 2018, 03:57

Hi all,

I stored about 18 lines of strings in clipboard, and tried to call InStr() to search the string, "this is me".
But InStr() always returned 0.

Here is the content stored in the clipboard:
Example 1
MsgBox % InStr("123abc789","abc") ; Returns 4

; Example 2
Haystack := "The Quick Brown Fox Jumps Over the Lazy Dog"
Needle := "Fox"
If InStr(Haystack, Needle)
MsgBox, The string was found.
Else
MsgBox, The string was not found.

; Example 3
Haystack := "The Quick Brown Fox Jumps Over the Lazy Dog"
Needle := "the"
MsgBox % InStr(Haystack, Needle, false, 1, 2) ; case insensitive search, return start position of second occurence
MsgBox % InStr(Haystack, Needle, true) ; case sensitive search, return start position of first occurence, same result as above
this is me

Code: Select all

!h::

Haystack := Clipboard
Needle := "this is me"
sleep, 1000
MsgBox % InStr(Haystack, Needle)

ESC::ExitApp
return
Can you tell me why it did not work please?
Thank you!

Kitty
gregster
Posts: 3745
Joined: 30 Sep 2013, 06:48

Re: When I tried to search a string in Clipboard, InStr() always returned 0

26 Jul 2018, 04:17

I cannot reproduce your problem. If I copy the text you showed, I get 585 and not 0 as the result in the msgbox...
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: When I tried to search a string in Clipboard, InStr() always returned 0

26 Jul 2018, 04:32

- Are you perhaps expecting that the clipboard variable will update to match the selected text? You need to send Ctrl+C to update the clipboard contents.
- Other issues could be different types of tab/space/whitespace (rather than spaces), and case sensitivity (what is the value of A_StringCaseSense). You could try the RegExMatch example below, to see if that works. You could also use MsgBox to report the value retrieved from the clipboard.

Code: Select all

q:: ;copy selected text, check if it contains string
Clipboard := ""
SendInput, ^c
ClipWait, 3
if ErrorLevel
{
	MsgBox, % "error: failed to retrieve clipboard text"
	return
}
vText := Clipboard

vNeedle := "this is me"
Sleep, 1000
MsgBox, % InStr(vText, vNeedle)
MsgBox, % RegExMatch(vText, "i)this.*is.*me")
return

;handy text for testing copying
;this is me
;this is not me
;this is not how I am
Btw

Code: Select all

!h::

Haystack := Clipboard
Needle := "this is me"
sleep, 1000
MsgBox % InStr(Haystack, Needle)
return ;LINE ADDED

ESC::ExitApp
;return ;LINE COMMENTED OUT
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 69 guests