WoW:USERAPI GetWords: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
No edit summary
 
m (Move page script moved page USERAPI GetWords to USERAPI GetWords without leaving a redirect)
 
(One intermediate revision by one other user not shown)
Line 45: Line 45:


__NOTOC__
__NOTOC__
[[Category:User Defined Functions]]
[[Category:User defined functions]]

Latest revision as of 04:49, 15 August 2023

This page documents a user-defined function that you can copy and paste into your addon. Replace PREFIX with your addon or lib prefix to avoid conflicts between different versions of these functions.

User defined functions

GetWords - by Mikk -

Split a string on spaces (one or more) and return as a table.

wordList = <PREFIX>_GetWords("string with space separated words")


Function Parameters

Arguments

str
String - containing words that you would like split

Returns

wordList
Table - array of words in the string


Example

wordList = <PREFIX>_GetWords("  string with   space   separated words");

Result

wordList[1]="string"
wordList[2]="with"
wordList[3]="space"
wordList[4]="separated"
wordList[5]="words"


Code

 function <PREFIX>_GetWords(str)
   local ret = {};
   local pos=0;
   while(true) do
     local word;
     _,pos,word=string.find(str, "^ *([^%s]+) *", pos+1);
     if(not word) then
       return ret;
     end
     table.insert(ret, word);
   end
 end