WoW:API strsplit: Difference between revisions
Jump to navigation
Jump to search
(Add note about multiple occurences of separator and add example using string.gmatch) |
mNo edit summary |
||
Line 12: | Line 12: | ||
:;delimiter : String - Delimiter | :;delimiter : String - Delimiter | ||
:; | :;subject : String - String to split | ||
Line 21: | Line 21: | ||
== 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(" | local a, b, c = strsplit(" ", "a b c") | ||
<big>'''Result'''</big> | <big>'''Result'''</big> |
Revision as of 16:59, 26 November 2007
Splits a string using a delimiter
s1, s2, s3 ... sn = strsplit("delimiter", "subject")
Arguments
- (delimiter, subject)
- delimiter
- String - Delimiter
- subject
- String - String to split
Returns
A list of strings. Not a table.
Example
local a, b, c = strsplit(" ", "a b c")
Result
a = "a" b = "b" c = "c"
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