Difference between revisions of "Manual:Lua Functions"
Line 122: | Line 122: | ||
Usage: | Usage: | ||
Example will print 'Troll is here!'. | Example will print 'Troll is here!'. | ||
+ | <syntaxhighlight> | ||
local str = string.trim(" Troll is here! ") | local str = string.trim(" Troll is here! ") | ||
echo("'" .. str .. "'") | echo("'" .. str .. "'") | ||
− | + | </syntaxhighlight> | |
+ | |||
==string:split== | ==string:split== | ||
Revision as of 14:41, 26 May 2011
Function Categories
Label/Window creation/manipulation Functions: These functions are used to construct custom user GUIs. They deal mainly with miniconsole/label/gauge creation and manipulation.
createMiniConsole()
createLabel()
createGauge()
Table Functions: These functions are used to manipulate tables. Through them you can add to tables, remove values, check if a value is present in the table, check the size of a table, and more.
String Functions
Scripting Object Functions
Mapper Functions
Miscellaneous Functions
Label/Window creation/manipulation Functions
Table Functions
String Functions
string.cut
string.cut(s, maxLen)
Cut string to specified maximum length.
Parameters:
s: maxLen:
Usage:
Following call will return 'abc'. string.cut("abcde", 3)
You can easily pad string to certain length. Example bellow will print 'abcde ' e.g. pad/cut string to 10 characters.
local s = "abcde"
s = string.cut(s .. " ", 10) -- append 10 spaces
echo("'" .. s .. "'")
string.enclose
string.enclose(s, maxlevel)
Enclose string by long brackets.
Parameters:
s: maxlevel:
string.ends
string.ends(String, Suffix)
Test if string is ending with specified suffix.
Parameters:
String: Suffix:
See also:
string.starts()
string.findPattern
string.findPattern(text, pattern) Return first matching substring or nil.
Parameters
text: pattern:
Usage: Following example will print: "I did find: Troll" string.
local match = string.findPattern("Troll is here!", "Troll")
if match then
echo("I did find: " .. match)
end
This example will find substring regardless of case.
local match = string.findPattern("Troll is here!", string.genNocasePattern("troll")) if match then echo("I did find: " .. match) end
Return value:
nil or first matching substring
See also:
string.genNocasePattern()
string.genNocasePattern
string.genNocasePattern(s)
Generate case insensitive search pattern from string.
Parameters
s:
Usage: Following example will generate and print "123[aA][bB][cC]" string.
echo(string.genNocasePattern("123abc"))
Return value:
case insensitive pattern string
string.starts
string.starts(String, Prefix) Test if string is starting with specified prefix. Parameters:
String: Prefix:
See also:
string.ends()
string.trim
string.trim (s) Trim string (remove all white spaces around string).
Parameters:
s:
Usage: Example will print 'Troll is here!'.
local str = string.trim(" Troll is here! ")
echo("'" .. str .. "'")
string:split
string:split(delimiter) Splits a string into a table by the given delimiter.
Parameters:
delimiter:
Usage: Split string by ", " delimiter.
names = "Alice, Bob, Peter" name_table = names:split(", ") display(name_table)
Previous code will print out:
table { 1: 'Alice' 2: 'Bob' 3: 'Peter' }
Return value:
array with split strings
string:title
string:title() Capitalize first character in a string.
Usage: Variable testname is now Anna.
testname = string.title("anna")
Example will set test to "Bob".
test = "bob" test = string.title(test)