Skip to content

str โ€‹

String operations for searching, splitting, trimming, and formatting text.

Usage โ€‹

lua
str = require "mods.str"

print(str.capitalize("hello world")) --> "Hello world"

Functions โ€‹

Formatting:

FunctionDescription
capitalize(s)Return copy with first character capitalized and the rest lowercased.
center(s, width, fillchar?)Center string within width, padded with fill characters.
count(s, sub, start?, stop?)Count non-overlapping occurrences of a substring.
endswith(s, suffix, start?, stop?)Return true if string ends with suffix.
expandtabs(s, tabsize?)Expand tabs to spaces using given tabsize.
find(s, sub, start?, stop?)Return lowest index of substring or nil if not found.
format_map(s, mapping)Format string with mapping (key-based) replacement.

Predicates:

FunctionDescription
isalnum(s)Return true if all characters are alphanumeric and string is non-empty.
isalpha(s)Return true if all characters are alphabetic and string is non-empty.
isascii(s)Return true if all characters are ASCII.
isdecimal(s)Return true if all characters are decimal characters and string is non-empty.
isdigit(s)Return true if all characters are digits and string is non-empty.
isidentifier(s)Return true if string is a valid identifier and not a reserved keyword.
islower(s)Return true if all cased characters are lowercase and there is at least one cased character.
isnumeric(s)Return true if all characters are numeric and string is non-empty.
isprintable(s)Return true if all characters are printable.
isspace(s)Return true if all characters are whitespace and string is non-empty.
istitle(s)Return true if string is titlecased.
isupper(s)Return true if all cased characters are uppercase and there is at least one cased character.

Layout:

FunctionDescription
join(sep, ls)Join an array-like table of strings using this string as separator.
ljust(s, width, fillchar?)Left-justify string in a field of given width.
lower(s)Return lowercased copy.
lstrip(s, chars?)Remove leading characters (default: whitespace).
rstrip(s, chars?)Remove trailing characters (default: whitespace).
strip(s, chars?)Remove leading and trailing characters (default: whitespace).

Split & Replace:

FunctionDescription
partition(s, sep)Partition string into head, sep, tail from left.
removeprefix(s, prefix)Remove prefix if present.
removesuffix(s, suffix)Remove suffix if present.
replace(s, old, new, count?)Return a copy of the string with all occurrences of a substring replaced.
rfind(s, sub, start?, stop?)Return highest index of substring or nil if not found.
rindex(s, sub, start?, stop?)Like rfind but raises an error when the substring is not found.
rjust(s, width, fillchar?)Right-justify string in a field of given width.
rpartition(s, sep)Partition string into head, sep, tail from right.
rsplit(s, sep?, maxsplit?)Split from the right by separator, up to maxsplit.
split(s, sep?, maxsplit?)Split by separator (or whitespace) up to maxsplit.
splitlines(s, keepends?)Split on line boundaries.

Casing & Transform:

FunctionDescription
swapcase(s)Return a copy with case of alphabetic characters swapped.
startswith(s, prefix, start?, stop?)Return true if string starts with prefix.
title(s)Return titlecased copy.
translate(s, table_map)Translate characters using a mapping table.
upper(s)Return uppercased copy.
zfill(s, width)Pad numeric string on the left with zeros.

Formatting โ€‹

capitalize(s) โ€‹

Return copy with first character capitalized and the rest lowercased.

Parameters:

  • s (string): Input string.

Return:

  • capitalized (string): Capitalized string.

Example:

lua
s = capitalize("hello WORLD") --> "Hello world"

center(s, width, fillchar?) โ€‹

Center string within width, padded with fill characters.

Parameters:

  • s (string): Input string.
  • width (integer): Target width.
  • fillchar? (string): Optional fill character.

Return:

  • centered (string): Centered string.

Example:

lua
s = center("hi", 6, "-") --> "--hi--"

count(s, sub, start?, stop?) โ€‹

Count non-overlapping occurrences of a substring.

Parameters:

  • s (string): Input string.
  • sub (string): Substring to search.
  • start? (integer): Optional start index (defaults to 1).
  • stop? (integer): Optional exclusive end index (defaults to #s + 1).

Return:

  • count (integer): Number of non-overlapping matches.

Example:

lua
n = count("aaaa", "aa")       --> 2
n = count("aaaa", "a", 2, -1) --> 2
n = count("abcd", "")         --> 5

endswith(s, suffix, start?, stop?) โ€‹

Return true if string ends with suffix.

Parameters:

  • s (string): Input string.
  • suffix (string|string[]): Suffix string.
  • start? (integer): Optional start index (defaults to 1).
  • stop? (integer): Optional exclusive end index (defaults to #s + 1).

Return:

  • hasSuffix (boolean): True when s ends with suffix.

Example:

lua
ok = endswith("hello.lua", ".lua") --> true

NOTE

If suffix is a list, returns true when any suffix matches.

expandtabs(s, tabsize?) โ€‹

Expand tabs to spaces using given tabsize.

Parameters:

  • s (string): Input string.
  • tabsize? (integer): Optional tab width (defaults to 8).

Return:

  • expanded (string): String with tabs expanded.

Example:

lua
s = expandtabs("a\tb", 4) --> "a   b"

find(s, sub, start?, stop?) โ€‹

Return lowest index of substring or nil if not found.

Parameters:

  • s (string): Input string.
  • sub (string): Substring to search.
  • start? (integer): Optional start index (defaults to 1).
  • stop? (integer): Optional exclusive end index (defaults to #s + 1).

Return:

  • index (integer?): First match index, or nil when not found.

Example:

lua
i = find("hello", "ll") --> 3

format_map(s, mapping) โ€‹

Format string with mapping (key-based) replacement.

Parameters:

  • s (string): Template string with {key} placeholders.
  • mapping (table): Values used to replace placeholder keys.

Return:

  • formatted (string): Formatted string with placeholders replaced.

Example:

lua
s = format_map("hi {name}", { name = "bob" }) --> "hi bob"

NOTE

format_map is a lightweight {key} replacement helper. For richer templating, use mods.template.

Predicates โ€‹

isalnum(s) โ€‹

Return true if all characters are alphanumeric and string is non-empty.

Parameters:

  • s (string): Input string.

Return:

  • isAlnum (boolean): True when s is non-empty and all characters are alphanumeric.

Example:

lua
ok = isalnum("abc123") --> true

NOTE

Lua letters are ASCII by default, so non-ASCII letters are not alphanumeric.

lua
isalnum("รก1") --> false

isalpha(s) โ€‹

Return true if all characters are alphabetic and string is non-empty.

Parameters:

  • s (string): Input string.

Return:

  • isAlpha (boolean): True when s is non-empty and all characters are alphabetic.

Example:

lua
ok = isalpha("abc") --> true

NOTE

Lua letters are ASCII by default, so non-ASCII letters are not alphabetic.

lua
isalpha("รก") --> false

isascii(s) โ€‹

Return true if all characters are ASCII.

Parameters:

  • s (string): Input string.

Return:

  • isAscii (boolean): True when all bytes in s are ASCII.

Example:

lua
ok = isascii("hello") --> true

NOTE

The empty string returns true.

isdecimal(s) โ€‹

Return true if all characters are decimal characters and string is non-empty.

Parameters:

  • s (string): Input string.

Return:

  • isDecimal (boolean): True when s is non-empty and all characters are decimal digits.

Example:

lua
ok = isdecimal("123") --> true

isdigit(s) โ€‹

Return true if all characters are digits and string is non-empty.

Parameters:

  • s (string): Input string.

Return:

  • isDigit (boolean): True when s is non-empty and all characters are digits.

Example:

lua
ok = isdigit("123") --> true

isidentifier(s) โ€‹

Return true if string is a valid identifier and not a reserved keyword.

Parameters:

  • s (string): Input string.

Return:

  • isIdentifier (boolean): True when s is a valid identifier and not a keyword.

Example:

lua
ok = isidentifier("foo_bar") --> true
ok = isidentifier("2var") --> false
ok = isidentifier("end") --> false (keyword)

islower(s) โ€‹

Return true if all cased characters are lowercase and there is at least one cased character.

Parameters:

  • s (string): Input string.

Return:

  • isLower (boolean): True when s has at least one cased character and all are lowercase.

Example:

lua
ok = islower("hello") --> true

isnumeric(s) โ€‹

Return true if all characters are numeric and string is non-empty.

Parameters:

  • s (string): Input string.

Return:

  • isNumeric (boolean): True when s is non-empty and all characters are numeric.

Example:

lua
ok = isnumeric("123") --> true

isprintable(s) โ€‹

Return true if all characters are printable.

Parameters:

  • s (string): Input string.

Return:

  • isPrintable (boolean): True when all bytes in s are printable ASCII.

Example:

lua
ok = isprintable("abc!") --> true

NOTE

The empty string returns true.

isspace(s) โ€‹

Return true if all characters are whitespace and string is non-empty.

Parameters:

  • s (string): Input string.

Return:

  • isSpace (boolean): True when s is non-empty and all characters are whitespace.

Example:

lua
ok = isspace(" \t") --> true

istitle(s) โ€‹

Return true if string is titlecased.

Parameters:

  • s (string): Input string.

Return:

  • isTitle (boolean): True when s is titlecased.

Example:

lua
ok = istitle("Hello World") --> true

isupper(s) โ€‹

Return true if all cased characters are uppercase and there is at least one cased character.

Parameters:

  • s (string): Input string.

Return:

  • isUpper (boolean): True when s has at least one cased character and all are uppercase.

Example:

lua
ok = isupper("HELLO") --> true

Layout โ€‹

join(sep, ls) โ€‹

Join an array-like table of strings using this string as separator.

Parameters:

  • sep (string): Separator value.
  • ls (string[]): Table value.

Return:

  • joined (string): Joined string.

Example:

lua
s = join(",", { "a", "b", "c" }) --> "a,b,c"

ljust(s, width, fillchar?) โ€‹

Left-justify string in a field of given width.

Parameters:

  • s (string): Input string.
  • width (integer): Target width.
  • fillchar? (string): Optional fill character.

Return:

  • leftJustified (string): Left-justified string.

Example:

lua
s = ljust("hi", 5, ".") --> "hi..."

lower(s) โ€‹

Return lowercased copy.

Parameters:

  • s (string): Input string.

Return:

  • lowercased (string): Lowercased string.

Example:

lua
s = lower("HeLLo") --> "hello"

lstrip(s, chars?) โ€‹

Remove leading characters (default: whitespace).

Parameters:

  • s (string): Input string.
  • chars? (string): Optional character set.

Return:

  • leadingStripped (string): String with leading characters removed.

Example:

lua
s = lstrip("  hello") --> "hello"

rstrip(s, chars?) โ€‹

Remove trailing characters (default: whitespace).

Parameters:

  • s (string): Input string.
  • chars? (string): Optional character set.

Return:

  • trailingStripped (string): String with trailing characters removed.

Example:

lua
s = rstrip("hello  ") --> "hello"

strip(s, chars?) โ€‹

Remove leading and trailing characters (default: whitespace).

Parameters:

  • s (string): Input string.
  • chars? (string): Optional character set.

Return:

  • stripped (string): String with leading and trailing characters removed.

Example:

lua
s = strip("  hello  ") --> "hello"

Split & Replace โ€‹

partition(s, sep) โ€‹

Partition string into head, sep, tail from left.

Parameters:

  • s (string): Input string.
  • sep (string): Separator value.

Return:

  • head (string): Part before the separator.
  • separator (string): Matched separator, or empty string when not found.
  • tail (string): Part after the separator.

Example:

lua
a, b, c = partition("a-b-c", "-") --> "a", "-", "b-c"

removeprefix(s, prefix) โ€‹

Remove prefix if present.

Parameters:

  • s (string): Input string.
  • prefix (string): Prefix string.

Return:

  • prefixRemoved (string): String with prefix removed when present.

Example:

lua
s = removeprefix("foobar", "foo") --> "bar"

removesuffix(s, suffix) โ€‹

Remove suffix if present.

Parameters:

  • s (string): Input string.
  • suffix (string): Suffix string.

Return:

  • suffixRemoved (string): String with suffix removed when present.

Example:

lua
s = removesuffix("foobar", "bar") --> "foo"

replace(s, old, new, count?) โ€‹

Return a copy of the string with all occurrences of a substring replaced.

Parameters:

  • s (string): Input string.
  • old (string): Substring to replace.
  • new (string): Replacement string.
  • count? (integer): Optional maximum replacement count.

Return:

  • replaced (string): String with replacements applied.

Example:

lua
s = replace("a-b-c", "-", "_", 1) --> "a_b-c"

rfind(s, sub, start?, stop?) โ€‹

Return highest index of substring or nil if not found.

Parameters:

  • s (string): Input string.
  • sub (string): Substring to search.
  • start? (integer): Optional start index (defaults to 1).
  • stop? (integer): Optional inclusive end index (defaults to #s).

Return:

  • index (integer?): Last match index, or nil when not found.

Example:

lua
i = rfind("ababa", "ba") --> 4

rindex(s, sub, start?, stop?) โ€‹

Like rfind but raises an error when the substring is not found.

Parameters:

  • s (string): Input string.
  • sub (string): Substring to search.
  • start? (integer): Optional start index (defaults to 1).
  • stop? (integer): Optional inclusive end index (defaults to #s).

Return:

  • index (integer): Last match index.

Example:

lua
i = rindex("ababa", "ba") --> 4

rjust(s, width, fillchar?) โ€‹

Right-justify string in a field of given width.

Parameters:

  • s (string): Input string.
  • width (integer): Target width.
  • fillchar? (string): Optional fill character.

Return:

  • rightJustified (string): Right-justified string.

Example:

lua
s = rjust("hi", 5, ".") --> "...hi"

rpartition(s, sep) โ€‹

Partition string into head, sep, tail from right.

Parameters:

  • s (string): Input string.
  • sep (string): Separator value.

Return:

  • head (string): Part before the separator.
  • separator (string): Matched separator, or empty string when not found.
  • tail (string): Part after the separator.

Example:

lua
a, b, c = rpartition("a-b-c", "-") --> "a-b", "-", "c"

rsplit(s, sep?, maxsplit?) โ€‹

Split from the right by separator, up to maxsplit.

Parameters:

  • s (string): Input string.
  • sep? (string): Optional separator value.
  • maxsplit? (integer): Optional maximum number of splits.

Return:

  • parts (mods.List): Split parts.

Example:

lua
parts = rsplit("a,b,c", ",", 1) --> { "a,b", "c" }

split(s, sep?, maxsplit?) โ€‹

Split by separator (or whitespace) up to maxsplit.

Parameters:

  • s (string): Input string.
  • sep? (string): Optional separator value.
  • maxsplit? (integer): Optional maximum number of splits.

Return:

  • parts (mods.List): Split parts.

Example:

lua
parts = split("a,b,c", ",") --> { "a", "b", "c" }

splitlines(s, keepends?) โ€‹

Split on line boundaries.

Parameters:

  • s (string): Input string.
  • keepends? (boolean): Optional whether to keep line endings.

Return:

  • lines (mods.List): Split lines.

Example:

lua
lines = splitlines("a\nb\r\nc") --> { "a", "b", "c" }

Casing & Transform โ€‹

swapcase(s) โ€‹

Return a copy with case of alphabetic characters swapped.

Parameters:

  • s (string): Input string.

Return:

  • swappedCase (string): String with alphabetic case swapped.

Example:

lua
s = swapcase("AbC") --> "aBc"

startswith(s, prefix, start?, stop?) โ€‹

Return true if string starts with prefix.

Parameters:

  • s (string): Input string.
  • prefix (string|string[]): Prefix string.
  • start? (integer): Optional start index (defaults to 1).
  • stop? (integer): Optional exclusive end index (defaults to #s + 1).

Return:

  • hasPrefix (boolean): True when s starts with prefix.

Example:

lua
ok = startswith("hello.lua", "he") --> true

NOTE

If prefix is a list, returns true when any prefix matches.

title(s) โ€‹

Return titlecased copy.

Parameters:

  • s (string): Input string.

Return:

  • titlecased (string): Titlecased string.

Example:

lua
s = title("hello world") --> "Hello World"

translate(s, table_map) โ€‹

Translate characters using a mapping table.

Parameters:

  • s (string): Input string.
  • table_map (table): Character translation map.

Return:

  • translated (string): Translated string.

Example:

lua
map = { [string.byte("a")] = "b", ["c"] = false }
s = translate("abc", map) --> "bb"

upper(s) โ€‹

Return uppercased copy.

Parameters:

  • s (string): Input string.

Return:

  • uppercased (string): Uppercased string.

Example:

lua
s = upper("Hello") --> "HELLO"

zfill(s, width) โ€‹

Pad numeric string on the left with zeros.

Parameters:

  • s (string): Input string.
  • width (integer): Target width.

Return:

  • zeroFilled (string): Zero-padded string.

Example:

lua
s = zfill("42", 5) --> "00042"