Check if string has Symbols

Get help with using AutoHotkey and its commands and hotkeys
User avatar
kyopw
Posts: 4
Joined: 16 Aug 2020, 20:28

Check if string has Symbols

16 Nov 2020, 20:09

hey hey everyone!! i really need a script to check if a string has any some specific symbol and/or some specific letter but i don't know how to do this

something like this:

Code: Select all

char := "abc"
string := "a"

if string contains any of char ;check if string has A, B or C; if some of the characters specified in char is find, then:
{
	MsgBox, string has char
}
i have tried InStr() but it doesn't works

Code: Select all

char := "!%"
string := "find!" ;i want to detect the '!' char
if InStr(string, char)
{
	MsgBox, found the char
}
someone know how to do this?
User avatar
mikeyww
Posts: 3967
Joined: 09 Sep 2014, 18:38

Re: Check if string has Symbols

16 Nov 2020, 20:24

Commas separate the items. See documentation.

Code: Select all

char := "a,b,c"
string := "a"
If string contains %char%
 MsgBox, String has char
Else MsgBox, Nope!

char := "!,%"
string := "find!" ;i want to detect the '!' char
If string contains %char%
 MsgBox, Yep!
Else MsgBox, Nope!
A comma-separated list of strings, each of which will be compared to the contents of Var for a match. Any spaces or tabs around the delimiting commas are significant, meaning that they are part of the match string.
User avatar
kyopw
Posts: 4
Joined: 16 Aug 2020, 20:28

Re: Check if string has Symbols

16 Nov 2020, 22:39

thanks! but i have tried:

Code: Select all

char := "],!"
string := "[abcde]"

if string contains %char%
{
	MsgBox, found
}
and it's not working, maybe cause the string is blank, how can i fix this?
User avatar
boiler
Posts: 6631
Joined: 21 Dec 2014, 02:44

Re: Check if string has Symbols

16 Nov 2020, 22:46

That script does work. Are you sure you saved your changes to the script before running it?
User avatar
boiler
Posts: 6631
Joined: 21 Dec 2014, 02:44

Re: Check if string has Symbols

16 Nov 2020, 22:52

kyopw wrote:
16 Nov 2020, 20:09
i have tried InStr() but it doesn't works

Code: Select all

char := "!%"
string := "find!" ;i want to detect the '!' char
if InStr(string, char)
{
	MsgBox, found the char
}
someone know how to do this?
The reason that doesn't work is that it is looking for the exact string !% to appear (both characters in a row). You can use RegExMatch with [ ] around the characters, which means to match any of the characters within the group:

Code: Select all

char := "[!%]"
string := "find!" ;i want to detect the '!' char
if RegExMatch(string, char)
{
	MsgBox, found the char
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot] and 33 guests