--[[-- 用指定字符或字符串分割输入字符串,返回包含分割结果的数组 local input = "Hello,World" local res = string.split(input, ",") -- res = {"Hello", "World"} local input = "Hello-+-World-+-Quick" local res = string.split(input, "-+-") -- res = {"Hello", "World", "Quick"} ]]
functionstring.split(input, delimiter) input = tostring(input) delimiter = tostring(delimiter) if (delimiter == '') thenreturnfalseend local pos, arr = 0, {} -- for each divider found for st, sp infunction()returnstring.find(input, delimiter, pos, true) enddo table.insert(arr, string.sub(input, pos, st - 1)) pos = sp + 1 end table.insert(arr, string.sub(input, pos)) return arr end