WoW:USERAPI ChunkSplit: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (New page: {{userfunc}} <!-- Leave this line in! --> <center>'''ChunkSplit''' ''- by egingell -''</center> Split a string into groups of "length" each ending with "endChars" (ident...)
 
mNo edit summary
Line 1: Line 1:
{{userfunc}} <!-- Leave this line in! -->
{{userfunc}} <!-- Leave this line in! -->
<center>'''ChunkSplit''' ''- by [[user:egingell|egingell]] -''</center>


Split a string into groups of "length" each ending with "endChars" (identical to the [http://php.net/chunk-split PHP function] of the same name).
Split a string into groups of "length" each ending with "endChars" (identical to the [http://php.net/chunk-split PHP function] of the same name).

Revision as of 16:37, 4 December 2007

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

Split a string into groups of "length" each ending with "endChars" (identical to the PHP function of the same name).

table = <PREFIX>_ChunkSplit(string [, length [, endChars]])


Function Parameters

Arguments

string
String - containing words that you would like split.
length
Integer - number of characters to split the string by. Default is 76.
endChars
String - Add this to the end of each chunk. Default is "\n".

Returns

chunks
Table - array of chunks


Example

chunks = <PREFIX>_ChunkSplit("World of Warcraft", 2)

Result

chunks = {
    "Wo\n",
    "rl\n",
    "d \n",
    "of\n",
    " W\n",
    "ar\n",
    "cr\n",
    "af\n",
    "t\n",
}

Code

function &lt;PREFIX&gt;_ChunkSplit(string, length, endChars)
    if not string then
        return {}
    end
    -- Sanity check: make sure length is an integer.
    length = floor(tonumber(length))
    if not length then
        length = 76
    end
    if not endChars then
        endChars = "\n"
    end
    local Table = {}
    for i=1, strlen(string), length do
        table.insert(Table, strsub(string, i, i + length) .. endChars)
    end
    return Table
end