string.split
Splits a string on a separator and returns the fields as an array.
Syntax
string.split(text: string, separator?: string) -> {string}
Parameters
| Parameter | Type | Description |
|---|---|---|
text | string | String to split. |
separator | string | Delimiter. Defaults to ",". |
Returns
| Type | Description |
|---|---|
{string} | Array of fields, starting at index 1. |
Description
Lua 5.4 has no string.split, so the VM adds the Luau version.
Empty fields are preserved, including a trailing empty field when the text ends with the separator.
An empty separator splits the string into single characters.
Example
local parts = string.split("a,b,,c", ",")
for i, value in ipairs(parts) do
print(i, value)
end
-- 1 a
-- 2 b
-- 3 (empty)
-- 4 c
Notes
string.split(s)— Splits on commas.string.split(s, "")— Splits into characters.string.split("", ",")— Returns a single empty field.