Difference between revisions of "Manual:String Functions"
m (moved Template:String Functions to Manual:String Functions: Manual:Basic Essentials) |
|||
Line 1: | Line 1: | ||
− | = String Functions = | + | == String Functions == |
− | ==string.byte== | + | ===string.byte=== |
;string.byte(string [, i [, j]]) | ;string.byte(string [, i [, j]]) | ||
;<nowiki>mystring:byte([, i [, j]])</nowiki> | ;<nowiki>mystring:byte([, i [, j]])</nowiki> | ||
: Returns the internal numerical codes of the characters <code>s[i], s[i+1], ···, s[j]</code>. The default value for <code>i</code> is <code>1</code>; the default value for <code>j</code> is <code>i</code>. | : Returns the internal numerical codes of the characters <code>s[i], s[i+1], ···, s[j]</code>. The default value for <code>i</code> is <code>1</code>; the default value for <code>j</code> is <code>i</code>. | ||
: Note that numerical codes are not necessarily portable across platforms. | : Note that numerical codes are not necessarily portable across platforms. | ||
− | : See also: [[ | + | : See also: [[#string.char|string.char]] |
+ | |||
;Example | ;Example | ||
<lua> | <lua> | ||
Line 14: | Line 15: | ||
</lua> | </lua> | ||
− | ==string.char== | + | ===string.char=== |
;string.char(···) | ;string.char(···) | ||
: Receives zero or more integers. Returns a string with length equal to the number of arguments, in which each character has the internal numerical code equal to its corresponding argument. | : Receives zero or more integers. Returns a string with length equal to the number of arguments, in which each character has the internal numerical code equal to its corresponding argument. | ||
: Note that numerical codes are not necessarily portable across platforms. | : Note that numerical codes are not necessarily portable across platforms. | ||
− | : See also: [[ | + | : See also: [[#string.byte|string.byte]] |
;Example | ;Example | ||
Line 24: | Line 25: | ||
--The following call will return the string "ABC" corresponding to the ASCII values 65, 66, 67 | --The following call will return the string "ABC" corresponding to the ASCII values 65, 66, 67 | ||
mystring = string.char(65, 66, 67) | mystring = string.char(65, 66, 67) | ||
− | |||
</lua> | </lua> | ||
− | ==string.cut== | + | ===string.cut=== |
;string.cut(string, maxLen) | ;string.cut(string, maxLen) | ||
: Cuts string to the specified maximum length. | : Cuts string to the specified maximum length. | ||
Line 48: | Line 48: | ||
</lua> | </lua> | ||
− | ==string.dump== | + | ===string.dump=== |
+ | ;string.dump() | ||
+ | : Need information here!!! | ||
+ | ;Example | ||
+ | '''Need example''' | ||
− | ==string.enclose== | + | ===string.enclose=== |
;string.enclose(String) | ;string.enclose(String) | ||
: Wraps a string with [[ ]] | : Wraps a string with [[ ]] | ||
Line 64: | Line 68: | ||
</lua> | </lua> | ||
− | ==string.ends== | + | ===string.ends=== |
;string.ends(String, Suffix) | ;string.ends(String, Suffix) | ||
: Test if string is ending with specified suffix. | : Test if string is ending with specified suffix. | ||
: Returns true or false. | : Returns true or false. | ||
− | : See also: [[ | + | : See also: [[#string.starts|string.starts]] |
;Parameters: | ;Parameters: | ||
Line 84: | Line 88: | ||
</lua> | </lua> | ||
− | ==string.find== | + | ===string.find=== |
+ | ;string.find() | ||
+ | : Need description | ||
+ | ;Example | ||
+ | '''Need example''' | ||
− | ==string.findPattern== | + | ===string.findPattern=== |
; string.findPattern(text, pattern) | ; string.findPattern(text, pattern) | ||
: Return first matching substring or nil. | : Return first matching substring or nil. | ||
Line 102: | Line 110: | ||
end | end | ||
</lua> | </lua> | ||
− | This example will find substring regardless of case. | + | :This example will find substring regardless of case. |
<lua>local match = string.findPattern("Troll is here!", string.genNocasePattern("troll")) | <lua>local match = string.findPattern("Troll is here!", string.genNocasePattern("troll")) | ||
if match then | if match then | ||
Line 110: | Line 118: | ||
* Return value: | * Return value: | ||
: nil or first matching substring | : nil or first matching substring | ||
− | See also: [[ | + | See also: [[#string.genNocasePattern|string.genNocasePattern()]] |
− | ==string.format== | + | ===string.format=== |
+ | ;string.format() | ||
+ | : Need description here. | ||
+ | ;Example | ||
+ | '''Need example''' | ||
− | ==string.genNocasePattern== | + | ===string.genNocasePattern=== |
; string.genNocasePattern(s) | ; string.genNocasePattern(s) | ||
: Generate case insensitive search pattern from string. | : Generate case insensitive search pattern from string. | ||
Line 127: | Line 139: | ||
: case insensitive pattern string | : case insensitive pattern string | ||
− | ==string.gfind== | + | ====string.gfind=== |
+ | ;string.gfind() | ||
+ | : Need description here. | ||
+ | ;Example | ||
+ | '''Need example''' | ||
− | ==string.gmatch== | + | ===string.gmatch=== |
+ | ;string.gmatch() | ||
+ | : Need description here. | ||
+ | ;Example | ||
+ | '''Need example''' | ||
− | ==string.gsub== | + | ===string.gsub=== |
+ | ;string.gsub() | ||
+ | : Need description here. | ||
+ | ;Example | ||
+ | '''Need example''' | ||
− | ==string.len== | + | ===string.len=== |
;string.len(String) | ;string.len(String) | ||
;<nowiki>mystring:len()</nowiki> | ;<nowiki>mystring:len()</nowiki> | ||
:Receives a string and returns its length. The empty string "" has length 0. Embedded zeros are counted, so "a\000bc\000" has length 5. | :Receives a string and returns its length. The empty string "" has length 0. Embedded zeros are counted, so "a\000bc\000" has length 5. | ||
− | ==string.lower== | + | ;Example |
+ | '''Need example''' | ||
+ | |||
+ | ===string.lower=== | ||
;string.lower(String) | ;string.lower(String) | ||
;<nowiki>mystring:lower()</nowiki> | ;<nowiki>mystring:lower()</nowiki> | ||
:Receives a string and returns a copy of this string with all uppercase letters changed to lowercase. All other characters are left unchanged. The definition of what an uppercase letter is depends on the current locale. | :Receives a string and returns a copy of this string with all uppercase letters changed to lowercase. All other characters are left unchanged. The definition of what an uppercase letter is depends on the current locale. | ||
− | : See also: [[ | + | : See also: [[#string.upper|string.upper]] |
+ | |||
+ | ;Example | ||
+ | '''Need example''' | ||
− | ==string.match== | + | ===string.match=== |
+ | ;string.match() | ||
+ | : Need description here. | ||
+ | ;Example | ||
+ | '''Need example''' | ||
− | ==string.rep== | + | ===string.rep=== |
;string.rep(String, n) | ;string.rep(String, n) | ||
;<nowiki>mystring:rep(n)</nowiki> | ;<nowiki>mystring:rep(n)</nowiki> | ||
: Returns a string that is the concatenation of <code>n</code> copies of the string <code>String</code>. | : Returns a string that is the concatenation of <code>n</code> copies of the string <code>String</code>. | ||
− | ==string.reverse== | + | ;Example |
+ | '''Need example''' | ||
+ | |||
+ | ===string.reverse=== | ||
;string.reverse(String) | ;string.reverse(String) | ||
;<nowiki>mystring:reverse()</nowiki> | ;<nowiki>mystring:reverse()</nowiki> | ||
Line 163: | Line 200: | ||
</lua> | </lua> | ||
− | ==string.split== | + | ===string.split=== |
;string.split(String, delimiter) | ;string.split(String, delimiter) | ||
;<nowiki>myString:split(delimiter)</nowiki> | ;<nowiki>myString:split(delimiter)</nowiki> | ||
Line 188: | Line 225: | ||
</lua> | </lua> | ||
− | Either method above will print out: | + | :Either method above will print out: |
− | + | : table { | |
− | + | :: 1: 'Alice' | |
− | + | :: 2: 'Bob' | |
− | + | :: 3: 'Peter' | |
− | + | : } | |
− | ==string.starts== | + | ===string.starts=== |
;string.starts(String, Prefix) | ;string.starts(String, Prefix) | ||
: Test if string is starting with specified prefix. | : Test if string is starting with specified prefix. | ||
Line 215: | Line 252: | ||
</lua> | </lua> | ||
− | ==string.sub== | + | ===string.sub=== |
+ | ;string.sub() | ||
+ | : Need description here. | ||
+ | ;Example | ||
+ | '''Need example''' | ||
− | ==string.title== | + | ===string.title=== |
;string.title(String) | ;string.title(String) | ||
;<nowiki>string:title()</nowiki> | ;<nowiki>string:title()</nowiki> | ||
Line 236: | Line 277: | ||
</lua> | </lua> | ||
− | ==string.trim== | + | ===string.trim=== |
;string.trim(String) | ;string.trim(String) | ||
: Trims String, removing all 'extra' white space at the beginning and end of the text. | : Trims String, removing all 'extra' white space at the beginning and end of the text. | ||
Line 252: | Line 293: | ||
</lua> | </lua> | ||
− | ==string.upper== | + | ===string.upper=== |
;string.upper(String) | ;string.upper(String) | ||
;<nowiki>mystring:upper()</nowiki> | ;<nowiki>mystring:upper()</nowiki> | ||
Line 258: | Line 299: | ||
: See also: [[Manual:Lua_Functions#string.lower|string.lower]] | : See also: [[Manual:Lua_Functions#string.lower|string.lower]] | ||
[[Category:Mudlet Manual]] | [[Category:Mudlet Manual]] | ||
+ | [[Category:Mudlet API]] |
Revision as of 22:14, 18 January 2012
String Functions
string.byte
- string.byte(string [, i [, j]])
- mystring:byte([, i [, j]])
- Returns the internal numerical codes of the characters
s[i], s[i+1], ···, s[j]
. The default value fori
is1
; the default value forj
isi
. - Note that numerical codes are not necessarily portable across platforms.
- See also: string.char
- Example
<lua> --The following call will return the ASCII values of "A", "B" and "C" a, b, c = string.byte("ABC", 1, 3)
echo(a .. " - " .. b .. " - " .. c) -- echos "65 - 66 - 67" </lua>
string.char
- string.char(···)
- Receives zero or more integers. Returns a string with length equal to the number of arguments, in which each character has the internal numerical code equal to its corresponding argument.
- Note that numerical codes are not necessarily portable across platforms.
- See also: string.byte
- Example
<lua> --The following call will return the string "ABC" corresponding to the ASCII values 65, 66, 67 mystring = string.char(65, 66, 67) </lua>
string.cut
- string.cut(string, maxLen)
- Cuts string to the specified maximum length.
- Returns the modified string.
- Parameters
- string:
- The text you wish to cut. Passed as a string.
- maxLen
- The maximum length you wish the string to be. Passed as an integer number.
- Example
<lua> --The following call will return 'abc' and store it in myString mystring = string.cut("abcde", 3) --You can easily pad string to certain length. Example below will print 'abcde ' e.g. pad/cut string to 10 characters. local s = "abcde" s = string.cut(s .. " ", 10) -- append 10 spaces echo("'" .. s .. "'") </lua>
string.dump
- string.dump()
- Need information here!!!
- Example
Need example
string.enclose
- string.enclose(String)
- Wraps a string with [[ ]]
- Returns the altered string.
- Parameters
- String: The string to enclose. Passed as a string.
- Example
<lua> --This will echo 'Oh noes!' to the main window echo("'" .. string.enclose("Oh noes!") .. "'") </lua>
string.ends
- string.ends(String, Suffix)
- Test if string is ending with specified suffix.
- Returns true or false.
- See also: string.starts
- Parameters
- String:
- The string to test. Passed as a string.
- Suffix:
- The suffix to test for. Passed as a string.
- Example
<lua> --This will test if the incoming line ends with "in bed" and if not will add it to the end. if not string.ends(line, "in bed") then
echo("in bed\n")
end </lua>
string.find
- string.find()
- Need description
- Example
Need example
string.findPattern
- string.findPattern(text, pattern)
- Return first matching substring or nil.
- Parameters
- text:
- pattern:
- Example
Following example will print: "I did find: Troll" string. <lua>
local match = string.findPattern("Troll is here!", "Troll") if match then echo("I did find: " .. match) end
</lua>
- This example will find substring regardless of case.
<lua>local match = string.findPattern("Troll is here!", string.genNocasePattern("troll")) if match then echo("I did find: " .. match) end </lua>
- Return value:
- nil or first matching substring
See also: string.genNocasePattern()
string.format
- string.format()
- Need description here.
- Example
Need example
string.genNocasePattern
- string.genNocasePattern(s)
- Generate case insensitive search pattern from string.
- Parameters
- s:
- Example
- Following example will generate and print "123[aA][bB][cC]" string.
<lua>echo(string.genNocasePattern("123abc"))</lua>
- Return value:
- case insensitive pattern string
=string.gfind
- string.gfind()
- Need description here.
- Example
Need example
string.gmatch
- string.gmatch()
- Need description here.
- Example
Need example
string.gsub
- string.gsub()
- Need description here.
- Example
Need example
string.len
- string.len(String)
- mystring:len()
- Receives a string and returns its length. The empty string "" has length 0. Embedded zeros are counted, so "a\000bc\000" has length 5.
- Example
Need example
string.lower
- string.lower(String)
- mystring:lower()
- Receives a string and returns a copy of this string with all uppercase letters changed to lowercase. All other characters are left unchanged. The definition of what an uppercase letter is depends on the current locale.
- See also: string.upper
- Example
Need example
string.match
- string.match()
- Need description here.
- Example
Need example
string.rep
- string.rep(String, n)
- mystring:rep(n)
- Returns a string that is the concatenation of
n
copies of the stringString
.
- Example
Need example
string.reverse
- string.reverse(String)
- mystring:reverse()
- Returns a string that is the string
String
reversed.
- Example
<lua> mystring = "Hello from Lua" echo(mystring:reverse()) -- displays 'auL morf olleH' </lua>
string.split
- string.split(String, delimiter)
- myString:split(delimiter)
- Splits a string into a table by the given delimiter. Can be called against a string (or variable holding a string) using the second form above.
- Returns a table containing the split sections of the string.
- Parameters
- String:
- The string to split. Parameter is not needed if using second form of the syntax above. Passed as a string.
- delimiter:
- The delimiter to use when splitting the string. Passed as a string.
- Example
<lua> -- This will split the string by ", " delimiter and print the resulting table to the main window. names = "Alice, Bob, Peter" name_table = string.split(names, ", ") display(name_table)
--The alternate method names = "Alice, Bob, Peter" name_table = names:split(", ") display(name_table) </lua>
- Either method above will print out:
- table {
- 1: 'Alice'
- 2: 'Bob'
- 3: 'Peter'
- }
string.starts
- string.starts(String, Prefix)
- Test if string is starting with specified prefix.
- Returns true or false
- See also: string.ends
- Parameters
- String:
- The string to test. Passed as a string.
- Prefix:
- The prefix to test for. Passed as a string.
- Example
<lua> --The following will see if the line begins with "You" and if so will print a statement at the end of the line if string.starts(line, "You") then
echo("====oh you====\n")
end </lua>
string.sub
- string.sub()
- Need description here.
- Example
Need example
string.title
- string.title(String)
- string:title()
- Capitalizes the first character in a string.
- Returns the altered string.
- Parameters
- String
- The string to modify. Not needed if you use the second form of the syntax above.
- Example
<lua> --Variable testname is now Anna. testname = string.title("anna") --Example will set test to "Bob". test = "bob" test = test:title() </lua>
string.trim
- string.trim(String)
- Trims String, removing all 'extra' white space at the beginning and end of the text.
- Returns the altered string.
- Parameters
- String:
- The string to trim. Passed as a string.
- Example
<lua> --This will print 'Troll is here!', without the extra spaces. local str = string.trim(" Troll is here! ") echo("'" .. str .. "'") </lua>
string.upper
- string.upper(String)
- mystring:upper()
- Receives a string and returns a copy of this string with all lowercase letters changed to uppercase. All other characters are left unchanged. The definition of what a lowercase letter is depends on the current locale.
- See also: string.lower