string_split

This function splits a string into separate strings using the given delimiter. The separated strings are returned in a new array.

The delimiter string is the boundary (either a single character or a string of characters) which causes the string to split.

For example, if the string is "This is the string", it can be split by using a single space character " " as the delimiter. The resulting array will look like this: ["This", "is", "the", "string"], with the original string being "split" wherever the delimiter was present.

The delimiter itself is never included in the resulting strings.

Also see: string_split_ext

 

Syntax:

string_split(string, delimiter, [remove_empty], [max_splits]);

Argument Type Description
string String The string to split using the given delimiter
delimiter String The delimiter to use
remove_empty Boolean OPTIONAL This parameter determines if empty array elements should be removed from the array or not (default is false). It can be useful for those situations where two delimiters are right next to each other in the string, with nothing in between. By default, in this case, an empty string is added to the array (representing the empty string between those two delimiters). If you don't want these empty strings in the array then you should set this parameter to true instead.
max_splits Real OPTIONAL This parameter determines the maximum number of splits to make. Any delimiters that come after max_splits become part of the last string, e.g. splitting "1|2|3|4|5" with a max_splits of 3 and | as the delimiter will return ["1", "2", "3", "4|5"].

 

Returns:

Array

 

Example 1:

file_path = "C:/Users/someone/Documents/data.json";

var _path_parts = string_split(file_path, "/");

show_debug_message(_path_parts);

drive_name = _path_parts[0];
file_name = array_last(_path_parts);

The above code first creates a string path that stores a path to a file. It then calls string_split on the path with a forward slash "/" as the delimiter and stores the array it returns in a temporary variable _path_parts.

Then it shows a debug message showing the contents of the _path_parts array. Finally it stores the first array entry (the drive letter) in a variable drive_name and the last array entry (the name of the file) in a variable file_name.

 

Example 2:

the_string = "abc|def||ghi|jkl|mno|pqrs|tuv|wxyz";
string_parts = string_split(the_string, "|", true, 5);

show_debug_message_ext("{0}, {1}, {2}, {3}, {4}", string_parts);

The above code creates a string the_string and splits it into a total of 5 (the value of max_splits) separate strings using string_split. By setting remove_empty to true, empty array elements such as the white space between the delimiter after "def" and the delimiter before "ghi" is first removed. Finally it displays a debug message using show_debug_message_ext, printing the first five slots in the array.