WoW:API strsplit: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
mNo edit summary
m (Move page script moved page API strsplit to API strsplit without leaving a redirect)
 
(4 intermediate revisions by 4 users not shown)
Line 2: Line 2:


<!-- Describe the purpose of the function, exhausting detail can be saved for a later section -->
<!-- Describe the purpose of the function, exhausting detail can be saved for a later section -->
Splits a string using a delimiter
Splits a string using a delimiter (optionally: into a specified number of pieces)
<!-- List return values and arguments as well as function name, follow Blizzard usage convention for args -->
<!-- List return values and arguments as well as function name, follow Blizzard usage convention for args -->
  s1, s2, s3 ... sn = strsplit("delimiter", "subject")
  s1, s2, s3 ... sn = strsplit("delimiter", "subject"[, pieces])




== Arguments ==
== Arguments ==
<!-- List each argument, together with its type -->
;delimiter : String - Characters (bytes) that will be interpreted as delimiter characters (bytes) in the string.
:(delimiter, subject)
;subject : String - String to split.
 
;pieces : Number (optional) - Maximum number of pieces to make (the "last" piece would contain the rest of the string); by default, an unbounded number of pieces is returned.
:;delimiter : String - Delimiter
:;subject : String - String to split
 


== Returns ==
== Returns ==
<!-- List each return value, together with its type -->
<!-- List each return value, together with its type -->
A list of strings. Not a table.
A list of strings. Not a table. If the delimiter is not found in the subject string, the whole subject string will be returned.


== Example ==
== Example ==
<!-- If it helps, include an example here, though it's not required if the usage is self-explanatory -->
<!-- If it helps, include an example here, though it's not required if the usage is self-explanatory -->
  local a, b, c = strsplit(" ", "a b c")
  local a, b, c = strsplit(" ", "a b c d", 3)


<big>'''Result'''</big>
<big>'''Result'''</big>
Line 27: Line 24:
  a = "a"
  a = "a"
  b = "b"
  b = "b"
  c = "c"
  c = "c d"


== Details ==
== Details ==
Line 40: Line 37:
   tinsert(tbl, v)
   tinsert(tbl, v)
  end
  end
Additionally note that the delimiter defines all bytes that will split the string, e.g.:
strsplit("ab", "1a2b3")  -- => "1", "2", "3"
or
strsplit("ab", "1ab2")  -- => "1", "", "2"


Nota Bene:  This function does not handle embedded NUL characters ("\0") gracefully.  If you need a unique "signpost" character embedded in your strings to be split apart later, try the ASCII bell character ("\a").  This won't show up in the game, and strsplit handles it just fine.
Nota Bene:  This function does not handle embedded NUL characters ("\0") gracefully.  If you need a unique "signpost" character embedded in your strings to be split apart later, try the ASCII bell character ("\a").  This won't show up in the game, and strsplit handles it just fine.

Latest revision as of 04:47, 15 August 2023

WoW API < strsplit

Splits a string using a delimiter (optionally: into a specified number of pieces)

s1, s2, s3 ... sn = strsplit("delimiter", "subject"[, pieces])


Arguments[edit]

delimiter
String - Characters (bytes) that will be interpreted as delimiter characters (bytes) in the string.
subject
String - String to split.
pieces
Number (optional) - Maximum number of pieces to make (the "last" piece would contain the rest of the string); by default, an unbounded number of pieces is returned.

Returns[edit]

A list of strings. Not a table. If the delimiter is not found in the subject string, the whole subject string will be returned.

Example[edit]

local a, b, c = strsplit(" ", "a b c d", 3)

Result

a = "a"
b = "b"
c = "c d"

Details[edit]

Again, note that the return from strsplit is a list of values, not a table. To get a table, use e.g.:

local tbl = { strsplit(delimiter, subject) }


Also note that strsplit uses a raw string as delimited, not a pattern, so it's not particularily well-suited for e.g. commandline arguments, where it should be ok to use multiple spaces. To extract whitespace-separated arguments, you can use e.g.

local tbl = {}
for v in string.gmatch(" this   has     lots of   space   ", "[^ ]+") do
  tinsert(tbl, v)
end

Additionally note that the delimiter defines all bytes that will split the string, e.g.:

strsplit("ab", "1a2b3")  -- => "1", "2", "3"

or

strsplit("ab", "1ab2")  -- => "1", "", "2"

Nota Bene: This function does not handle embedded NUL characters ("\0") gracefully. If you need a unique "signpost" character embedded in your strings to be split apart later, try the ASCII bell character ("\a"). This won't show up in the game, and strsplit handles it just fine.