API strsplit

104,671pages on
this wiki
Add New Page
Talk0 Share
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

String - Characters (bytes) that will be interpreted as delimiter characters (bytes) in the string.
String - String to split.
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)


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)

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

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


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.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki