# StrSplit() / StringSplit

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

## StrSplit() [v1.1.13+]

    Array := StrSplit(String , Delimiters, OmitChars)
    Array := StrSplit(String , Delimiters, OmitChars, MaxParts := -1)  ; [v1.1.28+]

### 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.

MaxParts [v1.1.28+]

  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.

### Return Value

This function returns an array (object) of strings.

### 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.MaxIndex()
    {
        this_color := ColorArray[A_Index]
        MsgBox, Color number %A_Index% is %this_color%.
    }

## StringSplit

Deprecated: This command is not recommended for use in new scripts. Use the StrSplit function instead.

    StringSplit, OutputArray, InputVar , Delimiters, OmitChars

### Parameters

OutputArray

  The name of the pseudo-array in which to store each substring extracted from InputVar. For example, if MyArray is specified, the command will put the number of substrings produced (0 if none) into MyArray0, the first substring into MyArray1, the second into MyArray2, and so on.

  Within a function, to create a pseudo-array that is global instead of local, declare MyArray0 as a global variable inside the function (the converse is true for assume-global functions). However, it is often also necessary to declare each element, due to a common source of confusion. For more details, see Functions.

InputVar

  The name of the variable whose contents will be analyzed. Do not enclose the name in percent signs unless you want the contents of the variable to be used as the name.

  Note: InputVar must not be one of the variables in OutputArray.

Delimiters

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

  Otherwise, Delimiters contains one or more characters (case sensitive), each of which is used to determine where the boundaries between substrings occur in InputVar. Since the delimiter characters are not considered to be part of the substrings themselves, they are never copied into OutputArray. Also, if there is nothing between a pair of delimiters within InputVar, the corresponding array element will be blank.

  For example: ``,` (an escaped comma) 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 InputVar.

  To use a string as a delimiter rather than a character, first use StringReplace to replace all occurrences of the string with a single character that is never used literally in the text. Consider this example, which uses the string <br> as a delimiter:

      StringReplace, NewHTML, HTMLString, <br>, ``, All  ; Replace each <br> with an accent.
      StringSplit, MyArray, NewHTML, ``  ; Split the string based on the accent character.

OmitChars

  An optional list of characters (case sensitive) to exclude from the beginning and end of each array element. For example, if OmitChars is `%A_Space%%A_Tab%`, 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.

  Unlike the last parameter of most other commands, commas in OmitChars must be escaped (`,).

### Remarks

If the array elements already exist, the command will change the values of only the first N elements, where N is the number of substrings present in InputVar. Any elements beyond N that existed beforehand will be unchanged. Therefore, it is safest to use the zero element (MyArray0) to determine how many items were actually produced by the command.

### Examples

    TestString := "This is a test."
    StringSplit, word_array, TestString, %A_Space%, .  ; Omits periods.
    MsgBox, The 4th word is %word_array4%.

    Colors := "red,green,blue"
    StringSplit, ColorArray, Colors, `,
    Loop, %ColorArray0%
    {
        this_color := ColorArray%A_Index%
        MsgBox, Color number %A_Index% is %this_color%.
    }

## Remarks

Whitespace characters such as spaces and tabs will be preserved unless those characters are themselves delimiters or included in OmitChars. Tabs and spaces can be trimmed from both ends of any variable by calling the Trim function. For example: `MyArray1 := Trim(MyArray1)`.

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 InputVar 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, IfInString, StringGetPos, StringMid, StringTrimLeft, StringTrimRight, StrLen() / StringLen, StringLower, StringUpper, StringReplace