Difference between revisions of "String.split"

From GiderosMobile
m (Text replacement - "<source" to "<syntaxhighlight")
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
Line 7: Line 7:
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
(table) = string.split(separator)
 
(table) = string.split(separator)
</source>
+
</syntaxhighlight>
  
 
  '''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"'''
Line 13: Line 13:
 
local values = input:split(",")
 
local values = input:split(",")
 
print(values[1], values[2], values[3])
 
print(values[1], values[2], values[3])
</source>
+
</syntaxhighlight>
  
 
  '''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'''
 
  '''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'''
Line 22: Line 22:
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
"" --> ""
 
"" --> ""
</source>
+
</syntaxhighlight>
  
 
==== Empty Slices ====
 
==== Empty Slices ====
Line 31: Line 31:
 
"," --> "", ""
 
"," --> "", ""
 
",," --> "", "", ""
 
",," --> "", "", ""
</source>
+
</syntaxhighlight>
  
 
==== Whitespace Preserved ====
 
==== Whitespace Preserved ====
Line 37: Line 37:
 
"  whitespace  " --> "  whitespace  "
 
"  whitespace  " --> "  whitespace  "
 
"foo , bar" --> "foo ", " bar"
 
"foo , bar" --> "foo ", " bar"
</source>
+
</syntaxhighlight>
  
 
==== Invalid UTF-8 ====
 
==== Invalid UTF-8 ====
Line 43: Line 43:
 
"\xFF" --> "\xFF"
 
"\xFF" --> "\xFF"
 
"\xFD,\xFE" --> "\xFD", "\xFE"
 
"\xFD,\xFE" --> "\xFD", "\xFE"
</source>
+
</syntaxhighlight>
  
 
==== Unicode ====
 
==== Unicode ====
Line 51: Line 51:
 
"•" --> U+2022 BULLET
 
"•" --> U+2022 BULLET
 
"hello•world" --> "hello", "world"
 
"hello•world" --> "hello", "world"
</source>
+
</syntaxhighlight>
  
 
=== Parameters ===
 
=== Parameters ===

Latest revision as of 14:33, 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.

(table) = string.split(separator)
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"
local values = input:split(",")
print(values[1], values[2], values[3])
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

"" --> ""

Empty Slices

"foo,,bar" --> "foo", "", "bar"
",foo" --> "", "foo"
"foo," --> "foo", ""
"," --> "", ""
",," --> "", "", ""

Whitespace Preserved

"   whitespace   " --> "   whitespace   "
"foo , bar" --> "foo ", " bar"

Invalid UTF-8

"\xFF" --> "\xFF"
"\xFD,\xFE" --> "\xFD", "\xFE"

Unicode

"," --> U+FF0C FULLWIDTH COMMA
"我很高兴,你呢?" --> "我很高兴", "你呢?"
"•" --> U+2022 BULLET
"hello•world" --> "hello", "world"

Parameters

separator: (string) separator character(s) to be used for splitting the string optional, default value ","

Return values

Returns (table) table of ordered results