Difference between revisions of "String.split"
m (Text replacement - "<source" to "<syntaxhighlight") |
|||
Line 5: | Line 5: | ||
=== Description === | === Description === | ||
Splits a string into parts based on the defined separator character(s), returning a table of ordered results. | Splits a string into parts based on the defined separator character(s), returning a table of ordered results. | ||
− | < | + | <syntaxhighlight lang="lua"> |
(table) = string.split(separator) | (table) = string.split(separator) | ||
</source> | </source> | ||
'''If an empty "slice" is located, that part will be returned as an empty string. For instance string.split("abc||def", "|") will return a table with three strings: "abc", "", and "def"''' | '''If an empty "slice" is located, that part will be returned as an empty string. For instance string.split("abc||def", "|") will return a table with three strings: "abc", "", and "def"''' | ||
− | < | + | <syntaxhighlight lang="lua"> |
local values = input:split(",") | local values = input:split(",") | ||
print(values[1], values[2], values[3]) | print(values[1], values[2], values[3]) | ||
Line 20: | Line 20: | ||
==== Empty String ==== | ==== Empty String ==== | ||
− | < | + | <syntaxhighlight lang="lua"> |
"" --> "" | "" --> "" | ||
</source> | </source> | ||
==== Empty Slices ==== | ==== Empty Slices ==== | ||
− | < | + | <syntaxhighlight lang="lua"> |
"foo,,bar" --> "foo", "", "bar" | "foo,,bar" --> "foo", "", "bar" | ||
",foo" --> "", "foo" | ",foo" --> "", "foo" | ||
Line 34: | Line 34: | ||
==== Whitespace Preserved ==== | ==== Whitespace Preserved ==== | ||
− | < | + | <syntaxhighlight lang="lua"> |
" whitespace " --> " whitespace " | " whitespace " --> " whitespace " | ||
"foo , bar" --> "foo ", " bar" | "foo , bar" --> "foo ", " bar" | ||
Line 40: | Line 40: | ||
==== Invalid UTF-8 ==== | ==== Invalid UTF-8 ==== | ||
− | < | + | <syntaxhighlight lang="lua"> |
"\xFF" --> "\xFF" | "\xFF" --> "\xFF" | ||
"\xFD,\xFE" --> "\xFD", "\xFE" | "\xFD,\xFE" --> "\xFD", "\xFE" | ||
Line 46: | Line 46: | ||
==== Unicode ==== | ==== Unicode ==== | ||
− | < | + | <syntaxhighlight lang="lua"> |
"," --> U+FF0C FULLWIDTH COMMA | "," --> U+FF0C FULLWIDTH COMMA | ||
"我很高兴,你呢?" --> "我很高兴", "你呢?" | "我很高兴,你呢?" --> "我很高兴", "你呢?" |
Revision as of 14:31, 13 July 2023
Available since: Gideros 2022.1
Class: string
Description
Splits a string into parts based on the defined separator character(s), returning a table of ordered results. <syntaxhighlight lang="lua"> (table) = string.split(separator) </source>
If an empty "slice" is located, that part will be returned as an empty string. For instance string.split("abc||def", "|") will return a table with three strings: "abc", "", and "def"
<syntaxhighlight lang="lua"> local values = input:split(",") print(values[1], values[2], values[3]) </source>
Also note that whitespace from the original string will be preserved, for example string.split("abc _ def", "_") will honor the whitespace on both sides of the _ separator. By default, the separator character is , but you can specify an alternative character or series of characters
Corner Cases
Empty String
<syntaxhighlight lang="lua"> "" --> "" </source>
Empty Slices
<syntaxhighlight lang="lua"> "foo,,bar" --> "foo", "", "bar" ",foo" --> "", "foo" "foo," --> "foo", "" "," --> "", "" ",," --> "", "", "" </source>
Whitespace Preserved
<syntaxhighlight lang="lua"> " whitespace " --> " whitespace " "foo , bar" --> "foo ", " bar" </source>
Invalid UTF-8
<syntaxhighlight lang="lua"> "\xFF" --> "\xFF" "\xFD,\xFE" --> "\xFD", "\xFE" </source>
Unicode
<syntaxhighlight lang="lua"> "," --> U+FF0C FULLWIDTH COMMA "我很高兴,你呢?" --> "我很高兴", "你呢?" "•" --> U+2022 BULLET "hello•world" --> "hello", "world" </source>
Parameters
separator: (string) separator character(s) to be used for splitting the string optional, default value ","
Return values
Returns (table) table of ordered results