strsplit

From Warcraft Wiki
(Redirected from API strsplittable)
Jump to navigation Jump to search
Wowprogramming.png  BTNTemp.png strsplit TheWarWithin-Icon-Inline.pngDragonflight-Icon-Inline.pngWrath-Logo-Small.pngWoW Icon update.png
BTNTemp.png strsplittable TheWarWithin-Icon-Inline.pngDragonflight-Icon-Inline.pngWrath-Logo-Small.pngWoW Icon update.png + 9.1.5

Splits a string using a delimiter.

s1, s2, ... = strsplit | string.split(delimiter, str [, pieces])
chunks = strsplittable(delimiter, str [, pieces])

Arguments

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

strsplit

s1, s2, ...
string - Returns a variable amount of string variables.

strsplittable

chunks
string[] - Returns an array of strings.

Example

/dump strsplit(":", "hello:world:banana:apple", 3)
> "hello", "world", "banana:apple"


/dump strsplittable(":", "hello:world:banana")
> {
	[1] = "hello",
	[2] = "world",
	[3] = "banana"
}

Details

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"

N.B. 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.