public final class StringLib extends Object
This library provides generic functions for string manipulation, such as finding and extracting substrings, and pattern matching. When indexing a string in Lua, the first character is at position 1 (not at 0, as in C). Indices are allowed to be negative and are interpreted as indexing backwards, from the end of the string. Thus, the last character is at position -1, and so on.
The string library provides all its functions inside the table string. It also sets
a metatable for strings where the __index field points to the string table.
Therefore, you can use the string functions in object-oriented style. For instance,
string.byte(s,i) can be written as s:byte(i).
The string library assumes one-byte character encodings.
| Modifier and Type | Method and Description |
|---|---|
static org.classdump.luna.runtime.LuaFunction |
byteFn()
Returns the function
string.byte. |
static org.classdump.luna.runtime.LuaFunction |
charFn()
Returns the function
string.char. |
static org.classdump.luna.runtime.LuaFunction |
dump()
Returns the function
string.dump. |
static org.classdump.luna.runtime.LuaFunction |
find()
Returns the function
string.find. |
static org.classdump.luna.runtime.LuaFunction |
format()
Returns the function
string.format. |
static org.classdump.luna.runtime.LuaFunction |
gmatch()
Returns the function
string.gmatch. |
static org.classdump.luna.runtime.LuaFunction |
gsub()
Returns the function
string.gsub. |
static void |
installInto(org.classdump.luna.StateContext context,
org.classdump.luna.Table env)
Installs the string library to the global environment
env in the state
context context. |
static org.classdump.luna.runtime.LuaFunction |
len()
Returns the function
string.len. |
static org.classdump.luna.runtime.LuaFunction |
lower()
Returns the function
string.lower. |
static org.classdump.luna.runtime.LuaFunction |
match()
Returns the function
string.match. |
static org.classdump.luna.runtime.LuaFunction |
rep()
Returns the function
string.rep. |
static org.classdump.luna.runtime.LuaFunction |
reverse()
Returns the function
string.reverse. |
static org.classdump.luna.runtime.LuaFunction |
sub()
Returns the function
string.sub. |
static org.classdump.luna.runtime.LuaFunction |
upper()
Returns the function
string.upper. |
public static org.classdump.luna.runtime.LuaFunction byteFn()
string.byte.
The following is the corresponding entry from the Lua Reference Manual:
string.byte (s [, i [, j]])Returns the internal numeric codes of the characters
s[i],s[i+1], ...,s[j]. The default value foriis 1; the default value forjisi. These indices are corrected following the same rules of function.string.subNumeric codes are not necessarily portable across platforms.
string.byte functionstring.bytepublic static org.classdump.luna.runtime.LuaFunction charFn()
string.char.
The following is the corresponding entry from the Lua Reference Manual:
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 numeric code equal to its corresponding argument.
Numeric codes are not necessarily portable across platforms.
string.char functionstring.charpublic static org.classdump.luna.runtime.LuaFunction dump()
string.dump.
The following is the corresponding entry from the Lua Reference Manual:
string.dump (function [, strip])Returns a string containing a binary representation (a binary chunk) of the given function, so that a later
on this string returns a copy of the function (but with new upvalues). Ifloadstripis a true value, the binary representation may not include all debug information about the function, to save space.Functions with upvalues have only their number of upvalues saved. When (re)loaded, those upvalues receive fresh instances containing nil. (You can use the debug library to serialize and reload the upvalues of a function in a way adequate to your needs.)
string.dump functionstring.dumppublic static org.classdump.luna.runtime.LuaFunction find()
string.find.
The following is the corresponding entry from the Lua Reference Manual:
string.find (s, pattern [, init [, plain]])Looks for the first match of
pattern(see §6.4.1) in the strings. If it finds a match, then find returns the indices ofswhere this occurrence starts and ends; otherwise, it returns nil. A third, optional numeric argumentinitspecifies where to start the search; its default value is 1 and can be negative. A value of true as a fourth, optional argumentplainturns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered magic. Note that ifplainis given, theninitmust be given as well.If the
patternhas captures, then in a successful match the captured values are also returned, after the two indices.
string.find functionstring.findpublic static org.classdump.luna.runtime.LuaFunction format()
string.format.
The following is the corresponding entry from the Lua Reference Manual:
string.format (formatstring, ···)Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string). The format string follows the same rules as the ISO C function
sprintf. The only differences are that the options/modifiers*,h,L,l,n, andpare not supported and that there is an extra option,q. Theqoption formats a string between double quotes, using escape sequences when necessary to ensure that it can safely be read back by the Lua interpreter. For instance, the callstring.format('%q', 'a string with "quotes" and \n new line')may produce the string:
"a string with \"quotes\" and \ new line"}Options
A,a,E,e,f,G, andgall expect a number as argument. Optionsc,d,i,o,u,X, andxexpect an integer. Optionqexpects a string. Optionsexpects a string; if its argument is not a string, it is converted to one following the same rules of. If the option has any modifier (flags, width, length), the string argument should not contain embedded zeros. When Lua is compiled with a non-C99 compiler, optionstostringAanda(hexadecimal floats) do not support any modifier (flags, width, length).
string.format functionstring.formatpublic static org.classdump.luna.runtime.LuaFunction gmatch()
string.gmatch.
The following is the corresponding entry from the Lua Reference Manual:
string.gmatch (s, pattern)Returns an iterator function that, each time it is called, returns the next captures from
pattern(see §6.4.1) over the strings. Ifpatternspecifies no captures, then the whole match is produced in each call.As an example, the following loop will iterate over all the words from string
s, printing one per line:s = "hello world from Lua" for w in string.gmatch(s, "%a+") do print(w) endThe next example collects all pairs
key=valuefrom 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 endFor this function, a caret '
^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.
string.gmatch functionstring.gmatchpublic static org.classdump.luna.runtime.LuaFunction gsub()
string.gsub.
The following is the corresponding entry from the Lua Reference Manual:
string.gsub (s, pattern, repl [, n])Returns a copy of
sin which all (or the firstn, if given) occurrences of the pattern (see §6.4.1) have been replaced by a replacement string specified byrepl, which can be a string, a table, or a function.gsubalso returns, as its second value, the total number of matches that occurred. The namegsubcomes from Global SUBstitution.If
replis a string, then its value is used for replacement. The character%works as an escape character: any sequence inreplof the form%d, withdbetween 1 and 9, stands for the value of thed-th captured substring. The sequence%0stands for the whole match. The sequence%%stands for a single%.If
replis a table, then the table is queried for every match, using the first capture as the key.If
replis a function, then this function is called every time a match occurs, with all captured substrings passed as arguments, in order.In any case, if the pattern specifies no captures, then it behaves as if the whole pattern was inside a capture.
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 load(s)() end) --> x="4+5 = 9"local t = {name="lua", version="5.3"} x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) --> x="lua-5.3.tar.gz"
string.gsub functionstring.gsubpublic static org.classdump.luna.runtime.LuaFunction len()
string.len.
The following is the corresponding entry from the Lua Reference Manual:
string.len (s)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.len functionstring.lenpublic static org.classdump.luna.runtime.LuaFunction lower()
string.lower.
The following is the corresponding entry from the Lua Reference Manual:
string.lower (s)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.
string.lower functionstring.lowerpublic static org.classdump.luna.runtime.LuaFunction match()
string.match.
The following is the corresponding entry from the Lua Reference Manual:
string.match (s, pattern [, init])Looks for the first match of
pattern(see §6.4.1) in the strings. If it finds one, thenmatchreturns the captures from the pattern; otherwise it returns nil. Ifpatternspecifies no captures, then the whole match is returned. A third, optional numeric argumentinitspecifies where to start the search; its default value is 1 and can be negative.
string.match functionstring.matchpublic static org.classdump.luna.runtime.LuaFunction rep()
string.rep.
The following is the corresponding entry from the Lua Reference Manual:
string.rep (s, n [, sep])Returns a string that is the concatenation of
ncopies of the stringsseparated by the stringsep. The default value forsepis the empty string (that is, no separator). Returns the empty string ifnis not positive.(Note that it is very easy to exhaust the memory of your machine with a single call to this function.)
string.rep functionstring.reppublic static org.classdump.luna.runtime.LuaFunction reverse()
string.reverse.
The following is the corresponding entry from the Lua Reference Manual:
string.reverse (s)Returns a string that is the string
sreversed.
string.reverse functionstring.reversepublic static org.classdump.luna.runtime.LuaFunction sub()
string.sub.
The following is the corresponding entry from the Lua Reference Manual:
string.sub (s, i [, j])Returns the substring of
sthat starts atiand continues untilj;iandjcan be negative. Ifjis absent, then it is assumed to be equal to -1 (which is the same as the string length). In particular, the callstring.sub(s,1,j)returns a prefix ofswith lengthj, andstring.sub(s, -i)returns a suffix ofswith lengthi.If, after the translation of negative indices,
iis less than 1, it is corrected to 1. Ifjis greater than the string length, it is corrected to that length. If, after these corrections,iis greater thanj, the function returns the empty string.
string.sub functionstring.subpublic static org.classdump.luna.runtime.LuaFunction upper()
string.upper.
The following is the corresponding entry from the Lua Reference Manual:
string.upper (s)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.
string.upper functionstring.upperpublic static void installInto(org.classdump.luna.StateContext context,
org.classdump.luna.Table env)
env in the state
context context.
If env.package.loaded is a table, adds the library table
to it with the key "string", using raw access.
context - the state context, must not be nullenv - the global environment, must not be nullNullPointerException - if context or env is nullCopyright © 2016–2017. All rights reserved.