字符串分割——string_split
脚本说明
该脚本用于指定分隔符切割字符串,该方法将返回一个数组,数组包含被切割出来的所有字符串片段
参数说明
参数序号 | 参数名 | 数据类型 | 参数说明 |
---|---|---|---|
arg0 | str | String | 要切割的字符串 |
arg1 | substr | String | 指定切割的分隔符 |
脚本代码
/// string_split(str, substr)
/* Description: divides string "str" at delimiter "substr" and
returns an array containing all of the pieces. */
var str = argument0, sub = argument1, result, i = 0, pos = string_pos(sub, str);
while(pos) {
result[i++] = string_delete(str, pos, string_length(str));
str = string_delete(str, 1, pos);
pos = string_pos(sub, str);
}
result[i] = str;
return result;
使用范例
var words = string_split("the sky is blue", " ");
show_debug_message(words[1]); // This will output "sky"
该范例中要求以空格——" "——来分割"the sky is blue"这段文字,并将返回的数组存入words变量中
words = ["the","sky","is","blue"]
最后用debug消息打印了"words[1]"即数组中第二个内容,最后会输出"sky"
本脚本转自GMLSCRIPT.COM,原作者ryuzaki_mrl
原帖地址:string_split
Comments