Explode String by Delimiter GML Script

Quick script to explode a string (such as a CSV line) into a DS List by whatever delimiter you want (usually a comma).

///explode(str, delimiter)
///@description Explodes a string into a DS List using a delimiter
///@param str
///@param delimiter

var str = argument0;
var del = argument1;

var list = ds_list_create();

var tmpString = "";
//Loop through string
for(var i = 1; i < string_length(str); i++){
    //if char at position is the delimiter, add to list
    if(string_char_at(str, i) == del){
        ds_list_add(list, tmpString);
        tmpString = "";
    } else { //add char at position to temporary string
        tmpString += string_char_at(str, i);
    }
}
//add final character to string and add to list
tmpString += string_char_at(str, i);
ds_list_add(list, tmpString);

//return the list
return list;

 

Leave a Reply

Your email address will not be published. Required fields are marked *