Gaming
 

API strsplit

From WoWWiki

Main Menu


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

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


Arguments

(delimiter, subject)
delimiter 
String - Delimiter
subject 
String - String to split
pieces 
integer - Optional argument designating the number of pieces to make.


Returns

A list of strings. Not a table.

Example

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

Result

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

Details

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

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.