Split: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
imported>Felice Enellen
(minor edit so I can make the comment I forgot "rollback" doesn't let me make: I removed the details about not needing parens for split in some cases, because that's not specific to split(), it's just a lua feature that you don't need parens if the call looks like func"" or func{}.)
imported>Thisismypassphrase
No edit summary
 
Line 11: Line 11:


When the separator is "", every character is split into a separate element.
When the separator is "", every character is split into a separate element.
The separator can also be a number (say, N), in which case - the function splits the string after every N characters.


== Examples ==
== Examples ==
Line 17: Line 19:
split("one:two:3",":",false) -- returns {"one","two","3"}
split("one:two:3",":",false) -- returns {"one","two","3"}
split("1,,2,") -- returns {1,"",2,""}
split("1,,2,") -- returns {1,"",2,""}
split("12345",2) -- returns (12,34,5)
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 23:28, 9 August 2022

split( str, [separator,] [convert_numbers] )
Split a string into a table of elements delimited by the given separator (defaults to ",").
str
The string.

separator
The separator (defaults to ",").

convert_numbers
When convert_numbers is true, numerical tokens are stored as numbers (defaults to true).

The split() function splits a string into a table of elements delimited by the given separator (defaults to ",").

Empty elements are stored as empty strings.

When the separator is "", every character is split into a separate element.

The separator can also be a number (say, N), in which case - the function splits the string after every N characters.

Examples

split("1,2,3") -- returns {1,2,3}
split("one:two:3",":",false) -- returns {"one","two","3"}
split("1,,2,") -- returns {1,"",2,""}
split("12345",2) -- returns (12,34,5)

See also