Version: beta
FUNCTION | |
---|---|
string.byte() | converts a character into its ASCII (decimal) equivalent |
string.char() | converts ASCII codes into their equivalent characters |
string.dump() | converts a function into binary |
string.find() | searches a string for a pattern |
string.format() | formats a string |
string.gmatch() | iterate over a string |
string.gsub() | substitute strings inside another string |
string.len() | return the length of a string |
string.lower() | converts a string to lower-case |
string.match() | searches a string for a pattern |
string.rep() | returns repeated copies of a string |
string.reverse() | reverses the order of characters in a string |
string.sub() | returns a substring of a string |
string.upper() | converts a string to upper-case |
string.byte(s,[i],[j])
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.
PARAMETERS
s |
|
[i] |
|
[j] |
string.char(...)
PARAMETERS
... |
string.dump(function)
PARAMETERS
function |
string.find(s,pattern,[init],[plain])
s
where this occurrence starts and ends;
otherwise, it returns nil.
A third, optional numerical argument init specifies
where to start the search;
its default value is 1 and can be negative.
A value of true as a fourth, optional argument plain
turns off the pattern matching facilities,
so the function does a plain "find substring" operation,
with no characters in pattern being considered "magic".
Note that if plain is given, then init must be given as well.
If the pattern has captures,
then in a successful match
the captured values are also returned,
after the two indices.
PARAMETERS
s |
|
pattern |
|
[init] |
|
[plain] |
string.format(formatstring,...)
*
, l, L, n, p,
and h are not supported
and that there is an extra option, q.
The q option formats a string in a form suitable to be safely read
back by the Lua interpreter:
the string is written between double quotes,
and all double quotes, newlines, embedded zeros,
and backslashes in the string
are correctly escaped when written.
For instance, the call
string.format('%q', 'a string with "quotes" and \n new line')
"a string with \"quotes\" and \ new line"
PARAMETERS
formatstring |
|
... |
string.gmatch(s,pattern)
s = "hello world from Lua" for w in string.gmatch(s, "%a+") do print(w) end
key=value
from the
given string into a table:
t = {} s = "from=world, to=Lua" for k, v in string.gmatch(s, "(%w+)=(%w+)") do t[k] = v end
PARAMETERS
s |
|
pattern |
string.gsub(s,pattern,repl,[n])
%
works as an escape character:
any sequence in repl of the form %n
,
with n between 1 and 9,
stands for the value of the n-th captured substring (see below).
The sequence %0
stands for the whole match.
The sequence %%
stands for a single %
.
If repl is a table, then the table is queried for every match,
using the first capture as the key;
if the pattern specifies no captures,
then the whole match is used as the key.
If repl is a function, then this function is called every time a
match occurs, with all captured substrings passed as arguments,
in order;
if the pattern specifies no captures,
then the whole match is passed as a sole argument.
If the value returned by the table query or by the function call
is a string or a number,
then it is used as the replacement string;
otherwise, if it is false or nil,
then there is no replacement
(that is, the original match is kept in the string).
Here are some examples:
x = string.gsub("hello world", "(%w+)", "%1 %1") --> x="hello hello world world" x = string.gsub("hello world", "%w+", "%0 %0", 1) --> x="hello hello world" x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") --> x="world hello Lua from" x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) --> x="home = /home/roberto, user = roberto" x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) return loadstring(s)() end) --> x="4+5 = 9" local t = {name="lua", version="5.1"} x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) --> x="lua-5.1.tar.gz"
PARAMETERS
s |
|
pattern |
|
repl |
|
[n] |
string.len(s)
""
has length 0.
Embedded zeros are counted,
so "a\000bc\000"
has length 5.
PARAMETERS
s |
string.lower(s)
PARAMETERS
s |
string.match(s,pattern,[init])
PARAMETERS
s |
|
pattern |
|
[init] |
string.rep(s,n)
PARAMETERS
s |
|
n |
string.reverse(s)
PARAMETERS
s |
string.sub(s,i,[j])
string.sub(s,1,j)
returns a prefix of s
with length j,
and string.sub(s, -i)
returns a suffix of s
with length i.
PARAMETERS
s |
|
i |
|
[j] |
string.upper(s)
PARAMETERS
s |