Difference between revisions of "Manual:String Functions"

From Mudlet
Jump to navigation Jump to search
Line 226: Line 226:
 
: The string to split. Parameter is not needed if using second form of the syntax above. Passed as a string.
 
: The string to split. Parameter is not needed if using second form of the syntax above. Passed as a string.
 
* ''delimiter:''
 
* ''delimiter:''
: The delimiter to use when splitting the string. Passed as a string.
+
: The delimiter to use when splitting the string. Passed as a string, and allows for [http://www.lua.org/pil/20.2.html Lua pattern types]. Use % to escape here (and %% to escape a stand-alone %).
  
 
;Example
 
;Example

Revision as of 22:19, 28 September 2013

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 for i is 1; the default value for j is i.
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 Note: 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()

Converts a function into a binary string. You can use the loadstring() function later to get the function back.

Example

<lua> string = string.dump(echo("this is a string")) --The following should then echo "this is a string" loadstring(string)() </lua>

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:
The text you are searching the pattern for.
  • pattern:
The pattern you are trying to find in the text.
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.
Parameters
  • String:
The string you want to find the length of. Passed as a string.
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 string String.
Example

Need example

string.reverse

string.reverse(string)
mystring:reverse()
Returns a string that is the string string reversed.
Parameters
  • string:
The string to reverse. Passed as a string.
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, and allows for Lua pattern types. Use % to escape here (and %% to escape a stand-alone %).
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.
Parameters
  • string:
The string you want to change to uppercase
Example

<lua> -- displays 'RUN BOB RUN' local str = string.upper("run bob run") </lua>

See also: string.lower