| Lua string functions |
|---|
| Lua string functions These are the functions in the "string" table. Indices, where used, start at 1 for the first character (not zero). Negative numbers count from the right, so -1 is the last character, -2 the second last, and so on. All strings have a metatable added to them by Lua with an __index entry pointing to the string table. What this means is that you can write string function calls in two ways: The second version is shorter as the word "string" is implied. Note the colon after the s, not a dot. string.byte (s, n) Returns the ASCII code for the nth character of the string s. The inverse operation is carried out by string.char. Default for n is 1. string.char (n1, n2, n3, ...) Receives 0 or more numbers and converts them to the corresponding characters. The inverse operation is carried out by string.byte. string.dump (f) Converts a function f into binary representation, which can be subsequently processed by loadstring to retrieve the function. The function must be a Lua function without upvalues. string.find (str, pattern, index, plain) Find the first match of the regular expression "pattern" in "str", starting at position "index". If found, returns the start and end position, and any captures as additional results. If not found, returns nil. If "plain" is true, the search string is plain text, not a regular expression. Also see string.match which operates in a similar way, but does not return the start and end positions. Patterns The standard patterns you can search for are: Important - the uppercase versions of the above represent the complement of the class. eg. %U represents everything except uppercase letters, %D represents everything except digits. There are some "magic characters" (such as %) that have special meanings. These are: If you want to use those in a pattern (as themselves) you must precede them by a % symbol. eg. %% would match a single % You can build your own pattern classes by using square brackets, eg. The repetition characters are: The standard "anchor" characters apply: You can also use round brackets to specify "captures": Here, whatever matches (.*) becomes the first pattern. You can also refer to matched substrings (captures) later on in an expression: This example shows how you can look for a repetition of a word matched earlier, whatever that word was ("dogs" in this case). As a special case, an empty capture string returns as the captured pattern, the position of itself in the string. eg. What this is saying is that the word "dogs" starts at column 9. Finally you can look for nested "balanced" things (such as parentheses) by using %b, like this: After %b you put 2 characters, which indicate the start and end of the balanced pair. If it finds a nested version it keeps processing until we are back at the top level. In this case the matching string was "(big fish (swimming) in the pond)". Examples of string.find: string.format (fstr, v1, v2, v3, ...) Formats the supplied values (v1, v2 etc.) using format string 'fstr', similar to the C function printf. It is an error to supply too few variables for the format string. The format string comprises literal text, and directives starting with %. Each directive controls the format of the next argument. Directives can include flags, width and precision controls. Directives can be:
You can optionally supply 'flags width.precision' arguments before the letter. Flags can be:
Width is the width of the returned field. If the converted number/string is wider than the width it is not truncated. You cannot use "*" as the width (as you can for printf). If you want variable-size strings you can simulate that by modifying the format string on-the-fly. eg. instead of %*g, use "%" .. width .. "g" Precision is the number of decimal places to show for floating-point numbers. string.gfind (str, pattern) This function is now called string.gmatch. Calling string.gfind raises an error. string.gmatch (str, pattern) Returns an iterator function for returning the next capture from a pattern over a string. If there is no capture, the whole match is produced. string.gsub (str, pattern, replacement, n) Returns a copy of str with matches to 'pattern' replaced by 'replacement', for a maximum of n times. As a second result it returns the number of matches made. 'replacement' can be a string in which case it simply replaces the matching pattern. However %1 through to %9 in the replacement pattern can refer to captured strings in the source pattern. %% becomes %. If 'replacement' is a function it is called for each match with the matching string as an argument. It should return a string which is the string to replace it with. If it returns nil the original string is retained. If 'replacement' is a table then the matching string is looked up in the table for each match, and if found, the replacement is substituted. string.len (str) Returns the length of the string, including any imbedded zero (0x00) bytes. You can also use #string to find its length. string.lower (str) Returns the string converted to lower-case. string.match (str, pattern, index) Find the first match of the regular expression "pattern" in "str", starting at position "index". If found, returns any captures in the pattern. If no captures were specified the entire matching string is returned. If not found, returns nil. This is similar to string.find, except that the starting and ending index are not returned. string.rep (str, n) Returns a string which is n copies of the source string concatenated together. string.reverse (str) Returns a string that is the string str reversed. string.sub (str, start, end) Returns a substring of the string, starting at index 'start' and ending at index 'end'. Both may be negative to indicate counting from the right. The end point is optional and defaults to -1, which is the entire rest of the string. string.upper (str) Returns the string converted to upper-case. See Also ... Topics
Lua base functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua math functions
Lua os functions
Lua package functions
Lua script extensions
Lua table functions
Regular Expressions(Help topic: general=lua_string)
Documentation contents page |