If var [not] in/contains value1,value2,...

AutoHotkey

If var [not] in/contains value1,value2,...

Checks whether a variable's contents match one of the items in a list.

if Var in MatchList
if Var not in MatchList

if Var contains MatchList
if Var not contains MatchList

Parameters

Var

The name of the variable whose contents will be checked. For the "in" operator, an exact match with one of the list items is required. For the "contains" operator, a match occurs more easily: whenever Var contains one of the list items as a substring.

MatchList

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. For example, if MatchList is set to ABC , XYZ then Var must contain either ABC with a trailing space or XYZ with a leading space to cause a match.

Two consecutive commas results in a single literal comma. For example, the following would produce a single literal comma at the end of string1: If Var In string1,,,string2. Similarly, the following list contains only a single item with a literal comma inside it: If Var In single,,item. To include a blank item in the list, make the first character a comma as in this example: If Var In ,string1,string2 (when using the "contains" operator, a blank item will always result in a match since the empty string is found in all strings).

Because the items in MatchList are not treated as individual parameters, the list can be contained entirely within a variable. In fact, all or part of it must be contained in a variable if its length exceeds 16383 since that is the maximum length of any script line. For example, MatchList might consist of %List1%,%List2%,%List3% -- where each of the sublists contains a large list of match phrases.

Any single item in the list that is longer than 16384 characters will have those extra characters treated as a new list item. Thus, it is usually best to avoid including such items.

Remarks

The comparison is always done alphabetically, not numerically. For example, the string "11" would not match the list item "11.0".

The "contains" operator is the same as using IfInString/IfNotInString except that multiple search strings are supported (any one of which will cause a match).

StringCaseSense On can be used to make the comparison case sensitive.

If MatchList is long, it can be broken up into several shorter lines by means of a continuation section, which might improve readability and maintainability.

Caution: The operators "between", "is", "in", and "contains" are not supported in expressions.

Related

if var between, IfEqual/Greater/Less, IfInString, StringCaseSense, Blocks, Else

Examples

if var in exe,bat,com
    MsgBox The file extension is an executable type.

if var in 1,2,3,5,7,11 ; Avoid spaces in list.
    MsgBox %var% is a small prime number.

if var contains 1,3  ; Note that it compares the values as strings, not numbers.
    MsgBox Var contains the digit 1 or 3 (Var could be 1, 3, 10, 21, 23, etc.)

if var in %MyItemList%
    MsgBox %var% is in the list.

InputBox, UserInput, Enter YES or NO
if UserInput not in yes,no
    MsgBox Your input is not valid.

WinGetTitle, active_title, A
if active_title contains Address List.txt,Customer List.txt
    MsgBox One of the desired windows is active.
if active_title not contains metapad,Notepad
    MsgBox But the file is not open in either Metapad or Notepad.