Home Explore Blog CI



neovim

63th chunk of `runtime/doc/luaref.txt`
39fa794b45c0abb56141658c2704ce43f6e59c2bd15229410000000100000fa9
 --> 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"
<

string.len({s})                                                   *string.len()*
        Receives a string and returns its length. The empty string `""` has
        length 0. Embedded zeros are counted, so `"a\000b\000c"` has length 5.

string.lower({s})                                               *string.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.

string.match({s}, {pattern} [, {init}])                         *string.match()*
        Looks for the first `match` of {pattern} in the string {s}. If it
        finds one, then `match` returns the captures from the pattern;
        otherwise it returns `nil`. If {pattern} specifies no captures, then
        the whole match is returned. A third, optional numerical argument
        {init} specifies where to start the search; its default value is 1 and
        may be negative.

string.rep({s}, {n})                                              *string.rep()*
        Returns a string that is the concatenation of {n} copies of the string
        {s}.

string.reverse({s})                                           *string.reverse()*
        Returns a string that is the string {s} reversed.

string.sub({s}, {i} [, {j}])                                      *string.sub()*
        Returns the substring of {s} that starts at {i} and continues until
        {j}; {i} and {j} may be negative. If {j} is absent, then it is assumed
        to be equal to `-1` (which is the same as the string length). In
        particular, the call `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}.

string.upper({s})                                               *string.upper()*
        Receives a string and returns a copy of that 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.

------------------------------------------------------------------------------
5.4.1  Patterns                                                  *lua-pattern*

A character class is used to represent a set of characters. The following
combinations are allowed in describing a character class:

    - `x`   (where `x` is not one of the magic characters `^$()%.[]*+-?`)
          represents the character `x` itself.
    - `.`   (a dot) represents all characters.
    - `%a`  represents all letters.
    - `%c`  represents all control characters.
    - `%d`  represents all digits.
    - `%l`  represents all lowercase letters.
    - `%p`  represents all punctuation characters.
    - `%s`  represents all space characters.
    - `%u`  represents all uppercase letters.
    - `%w`  represents all alphanumeric characters.
    - `%x`  represents all hexadecimal digits.
    - `%z`  represents the character with representation `0`.
    - `%x`  (where `x` is any non-alphanumeric character) represents the
          character `x`. This is the standard way to escape the magic
          characters. Any punctuation character (even the non-magic) can be
          preceded by a `%` when used to represent itself in a pattern.

    - `[set]`  represents the class which is the union of all characters in
         `set`. A range of characters may be specified

Title: Lua String Library: len, lower, match, rep, reverse, sub, upper, and Patterns
Summary
This section describes several functions in the Lua string library: `string.len` (returns string length), `string.lower` (converts to lowercase), `string.match` (finds the first match of a pattern), `string.rep` (repeats a string n times), `string.reverse` (reverses a string), `string.sub` (extracts a substring), and `string.upper` (converts to uppercase). The section also introduces Lua patterns, including character classes like `%a` for letters, `%d` for digits, and how to represent sets of characters.