luaguides

Reference

String Methods

Methods for string manipulation.

  1. string.byte

    Return the numeric byte codes of a Lua 5.4 string. Supports negative indices and returns one integer per byte in the range.

  2. string.char

    string.char in Lua 5.4 takes zero or more integers and returns a string whose bytes are those integer codes. Inverse of string.byte for raw byte sequences.

  3. string.dump

    `string.dump` serializes a Lua function to a binary chunk string that `load()` can rebuild for fast startup, bytecode caching, and shipping precompiled code.

  4. string.find

    string.find searches a Lua string for a pattern match, returning start and end positions. Supports plain literal search to bypass pattern syntax.

  5. string.format()

    string.format() in Lua 5.4 substitutes printf-style %-directives into a string — covers flags, width, precision, specifiers, examples, and pitfalls.

  6. string.gmatch

    string.gmatch returns an iterator that yields every non-overlapping match of a Lua pattern, in order, with captures unpacked for for-in loops.

  7. string.gsub

    string.gsub substitutes pattern matches in Lua strings, returns transformed string and count. Supports string, table, and function replacements.

  8. string.len

    string.len returns the byte length of a Lua 5.4 string. Equivalent to the # operator, it counts embedded NUL bytes and is the canonical way to size a string.

  9. string.lower

    string.lower returns a copy of a string with all ASCII uppercase letters replaced by their lowercase equivalents. Locale-driven; not UTF-8 aware.

  10. string.match

    string.match returns the captures from the first match of a Lua pattern in a string, or the whole match when no captures are defined.

  11. string.pack

    Pack Lua values into a binary string with a format string. Lua 5.4 string.pack reference: every option, alignment rules, and worked examples.

  12. string.packsize

    string.packsize returns the byte length that string.pack would produce for a given format string, without packing any values or allocating a buffer.

  13. string.rep

    string.rep returns a string made of n copies of s, optionally joined by sep, or an empty string when n is not positive.

  14. string.reverse

    Return a copy of a string with its bytes in reverse order. Covers palindrome checks, UTF-8 caveats, and method syntax.

  15. string.sub

    string.sub returns a substring of a Lua 5.4 string by 1-based inclusive byte indices, with negative indices counting back from the end.

  16. string.unpack

    string.unpack reads a binary string produced by string.pack and decodes it into Lua values, with optional start position and a trailing offset for streaming.

  17. string.upper

    string.upper returns a copy of a string with all ASCII lowercase letters replaced by their uppercase equivalents. Locale-driven; not UTF-8 aware.