Separates a string into an array of substrings using the specified delimiters.
Array := StrSplit(String , Delimiters, OmitChars, MaxParts := -1)
A string to split.
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.
"," 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.
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.
The maximum number of substrings to return. If non-zero, the string is split a maximum of MaxParts-1 times and the remainder of the string is returned in the last substring (excluding any leading or trailing OmitChars). If omitted or -1, there is no limit.
This function returns an array containing the substrings of the specified string.
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 function.
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
Parsing loop, Arrays, Sort, SplitPath, InStr, SubStr, StrLen, StrLower, StrUpper, StrReplace
TestString := "This is a test." word_array := StrSplit(TestString, A_Space, ".") ; Omits periods. MsgBox("The 4th word is " word_array)
Colors := "red,green,blue" For i, color in StrSplit(Colors, ",") MsgBox "Color number " i " is " color