StrSplit()

Auto Hotkey

StrSplit

Separates a string into an array of substrings using the specified delimiters.

Array := StrSplit(String , Delimiters, OmitChars)

Parameters

String

A string to split.

Delimiters

If this parameter is blank or omitted, each character of the input string will be treated as a separate substring.

Otherwise, Delimiters can be either a single string or an array of strings, each of which is used to determine where the boundaries between substrings occur. Since the delimiters are not considered to be part of the substrings themselves, they are never included in the returned array. Also, if there is nothing between a pair of delimiters within the input string, the corresponding array element will be blank.

For example: "," would divide the string based on every occurrence of a comma. Similarly, [A_Tab, A_Space] would create a new array element every time a space or tab is encountered in the input string.

OmitChars

An optional list of characters (case sensitive) to exclude from the beginning and end of each array element. For example, if OmitChars is " `t", spaces and tabs will be removed from the beginning and end (but not the middle) of every element.

If Delimiters is blank, OmitChars indicates which characters should be excluded from the array.

Remarks

Whitespace characters such as spaces and tabs will be preserved unless those characters are included in the Delimiters or OmitChars parameters. Tabs and spaces can be trimmed from both ends of any variable by using Trim. For example: var := Trim(var)

To split a string that is in standard CSV (comma separated value) format, use a parsing loop since it has built-in CSV handling.

To arrange the fields in a different order prior to splitting them, use the Sort command.

If you do not need the substrings to be permanently stored in memory, consider using a parsing loop -- especially if the input string is very large, in which case a large amount of memory would be saved. For example:

Colors := "red,green,blue"
Loop, parse, %Colors%, `,
    MsgBox Color number %A_Index% is %A_LoopField%.

Related

Parsing loop, Arrays, Sort, SplitPath, InStr, SubStr, StrLen, StrLower, StrUpper, StrReplace

Examples

TestString := "This is a test."
word_array := StrSplit(TestString, A_Space, ".")  ; Omits periods.
MsgBox("The 4th word is " word_array[4])

Colors := "red,green,blue"
ColorArray := StrSplit(Colors, ",")
Loop ColorArray.Length()
{
    this_color := ColorArray[a_index]
    MsgBox, Color number %a_index% is %this_color%.
}