WoW:API strsplit: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(New page: {{wowapi}} __NOTOC__ <!-- Describe the purpose of the function, exhausting detail can be saved for a later section --> One line summary description of function. <!-- List return values an...)
 
(Add note about multiple occurences of separator and add example using string.gmatch)
Line 30: Line 30:


== Details ==
== Details ==
<!-- Details not appropriate for the main description can go here.
    REMOVE the section if you're just going to restate the intro line! -->


To get a table use:
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) }
  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

Revision as of 17:00, 19 October 2007

WoW API < strsplit

Splits a string using a delimiter

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


Arguments

(delimiter, subject)
delimiter
String - Delimiter
subejct
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