This helper splits a string using a literal delimiter and returns the pieces in an array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| function string.split(input, delimiter)
input = tostring(input)
delimiter = tostring(delimiter)
if delimiter == '' then
return false
end
local position = 1
local result = {}
while true do
local startPos, endPos = string.find(
input,
delimiter,
position,
true
)
if not startPos then
table.insert(result, string.sub(input, position))
break
end
table.insert(result, string.sub(input, position, startPos - 1))
position = endPos + 1
end
return result
end
|
Examples:
1
2
3
4
5
| string.split("Hello,World", ",")
-- {"Hello", "World"}
string.split("Hello-+-World-+-Quick", "-+-")
-- {"Hello", "World", "Quick"}
|
The fourth argument to string.find is true, so the delimiter is treated as plain text rather than a Lua pattern.