| description | String utility helpers modeled after Python's `str`. |
|---|
String utility helpers modeled after Python's str.
str = require "mods.str"
print(str.capitalize("hello world")) --> "Hello world"Formatting:
| Function | Description |
|---|---|
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:
| Function | Description |
|---|---|
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:
| Function | Description |
|---|---|
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:
| Function | Description |
|---|---|
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:
| Function | Description |
|---|---|
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. |
Return copy with first character capitalized and the rest lowercased.
Parameters:
s(string): Input string.
Return:
s(string): Computed string value.
Example:
s = capitalize("hello WORLD") --> "Hello world"Center string within width, padded with fill characters.
Parameters:
s(string): Input string.width(integer): Target width.fillchar?(string): Optional fill character.
Return:
s(string): Computed string value.
Example:
s = center("hi", 6, "-") --> "--hi--"Count non-overlapping occurrences of a substring.
Parameters:
s(string): Input string.sub(string): Substring to search.start?(integer): Optional start index (defaults to1).stop?(integer): Optional exclusive end index (defaults to#s + 1).
Return:
n(integer): Computed numeric value.
Example:
n = count("aaaa", "aa") --> 2
n = count("aaaa", "a", 2, -1) --> 2
n = count("abcd", "") --> 5Return true if string ends with suffix.
Parameters:
s(string): Input string.suffix(string|string[]): Suffix string.start?(integer): Optional start index (defaults to1).stop?(integer): Optional exclusive end index (defaults to#s + 1).
Return:
ok(boolean): True whensends withsuffix.
Example:
ok = endswith("hello.lua", ".lua") --> trueNote
If suffix is a list, returns true when any suffix matches.
Expand tabs to spaces using given tabsize.
Parameters:
s(string): Input string.tabsize?(integer): Optional tab width (defaults to8).
Return:
s(string): Computed string value.
Example:
s = expandtabs("a\tb", 4) --> "a b"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 to1).stop?(integer): Optional exclusive end index (defaults to#s + 1).
Return:
index(integer?): First match index, ornilwhen not found.
Example:
i = find("hello", "ll") --> 3Format string with mapping (key-based) replacement.
Parameters:
s(string): Template string with{key}placeholders.mapping(table): Values used to replace placeholder keys.
Return:
s(string): Formatted string with placeholders replaced.
Example:
s = format_map("hi {name}", { name = "bob" }) --> "hi bob"Note
format_map is a lightweight {key} replacement helper. For richer
templating, use mods.template.
Return true if all characters are alphanumeric and string is non-empty.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whensis non-empty and all characters are alphanumeric.
Example:
ok = isalnum("abc123") --> trueNote
Lua letters are ASCII by default, so non-ASCII letters are not alphanumeric.
isalnum("á1") --> falseReturn true if all characters are alphabetic and string is non-empty.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whensis non-empty and all characters are alphabetic.
Example:
ok = isalpha("abc") --> trueNote
Lua letters are ASCII by default, so non-ASCII letters are not alphabetic.
isalpha("á") --> falseReturn true if all characters are ASCII.
Parameters:
s(string): Input string.
Return:
ok(boolean): True when all bytes insare ASCII.
Example:
ok = isascii("hello") --> trueNote
The empty string returns true.
Return true if all characters are decimal characters and string is non-empty.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whensis non-empty and all characters are decimal digits.
Example:
ok = isdecimal("123") --> trueReturn true if all characters are digits and string is non-empty.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whensis non-empty and all characters are digits.
Example:
ok = isdigit("123") --> trueReturn true if string is a valid identifier and not a reserved keyword.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whensis a valid identifier and not a keyword.
Example:
ok = isidentifier("foo_bar") --> true
ok = isidentifier("2var") --> false
ok = isidentifier("end") --> false (keyword)Return true if all cased characters are lowercase and there is at least one cased character.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whenshas at least one cased character and all are lowercase.
Example:
ok = islower("hello") --> trueReturn true if all characters are numeric and string is non-empty.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whensis non-empty and all characters are numeric.
Example:
ok = isnumeric("123") --> trueReturn true if all characters are printable.
Parameters:
s(string): Input string.
Return:
ok(boolean): True when all bytes insare printable ASCII.
Example:
ok = isprintable("abc!") --> trueNote
The empty string returns true.
Return true if all characters are whitespace and string is non-empty.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whensis non-empty and all characters are whitespace.
Example:
ok = isspace(" \t") --> trueReturn true if string is titlecased.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whensis titlecased.
Example:
ok = istitle("Hello World") --> trueReturn true if all cased characters are uppercase and there is at least one cased character.
Parameters:
s(string): Input string.
Return:
ok(boolean): True whenshas at least one cased character and all are uppercase.
Example:
ok = isupper("HELLO") --> trueJoin an array-like table of strings using this string as separator.
Parameters:
sep(string): Separator value.ls(string[]): Table value.
Return:
s(string): Computed string value.
Example:
s = join(",", { "a", "b", "c" }) --> "a,b,c"Left-justify string in a field of given width.
Parameters:
s(string): Input string.width(integer): Target width.fillchar?(string): Optional fill character.
Return:
s(string): Computed string value.
Example:
s = ljust("hi", 5, ".") --> "hi..."Return lowercased copy.
Parameters:
s(string): Input string.
Return:
s(string): Computed string value.
Example:
s = lower("HeLLo") --> "hello"Remove leading characters (default: whitespace).
Parameters:
s(string): Input string.chars?(string): Optional character set.
Return:
s(string): Computed string value.
Example:
s = lstrip(" hello") --> "hello"Remove trailing characters (default: whitespace).
Parameters:
s(string): Input string.chars?(string): Optional character set.
Return:
s(string): Computed string value.
Example:
s = rstrip("hello ") --> "hello"Remove leading and trailing characters (default: whitespace).
Parameters:
s(string): Input string.chars?(string): Optional character set.
Return:
s(string): Computed string value.
Example:
s = strip(" hello ") --> "hello"Partition string into head, sep, tail from left.
Parameters:
s(string): Input string.sep(string): Separator value.
Return:
head(string): Part before the separator.sep_found(string): Matched separator, or empty string when not found.tail(string): Part after the separator.
Example:
a, b, c = partition("a-b-c", "-") --> "a", "-", "b-c"Remove prefix if present.
Parameters:
s(string): Input string.prefix(string): Prefix string.
Return:
s(string): Computed string value.
Example:
s = removeprefix("foobar", "foo") --> "bar"Remove suffix if present.
Parameters:
s(string): Input string.suffix(string): Suffix string.
Return:
s(string): Computed string value.
Example:
s = removesuffix("foobar", "bar") --> "foo"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:
s(string): Computed string value.
Example:
s = replace("a-b-c", "-", "_", 1) --> "a_b-c"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 to1).stop?(integer): Optional inclusive end index (defaults to#s).
Return:
index(integer?): Last match index, ornilwhen not found.
Example:
i = rfind("ababa", "ba") --> 4Like 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 to1).stop?(integer): Optional inclusive end index (defaults to#s).
Return:
index(integer): Computed numeric value.
Example:
i = rindex("ababa", "ba") --> 4Right-justify string in a field of given width.
Parameters:
s(string): Input string.width(integer): Target width.fillchar?(string): Optional fill character.
Return:
s(string): Computed string value.
Example:
s = rjust("hi", 5, ".") --> "...hi"Partition string into head, sep, tail from right.
Parameters:
s(string): Input string.sep(string): Separator value.
Return:
head(string): Part before the separator.sep_found(string): Matched separator, or empty string when not found.tail(string): Part after the separator.
Example:
a, b, c = rpartition("a-b-c", "-") --> "a-b", "-", "c"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:
parts = rsplit("a,b,c", ",", 1) --> { "a,b", "c" }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:
parts = split("a,b,c", ",") --> { "a", "b", "c" }Split on line boundaries.
Parameters:
s(string): Input string.keepends?(boolean): Optional whether to keep line endings.
Return:
lines(mods.List): Split lines.
Example:
lines = splitlines("a\nb\r\nc") --> { "a", "b", "c" }Return a copy with case of alphabetic characters swapped.
Parameters:
s(string): Input string.
Return:
s(string): Computed string value.
Example:
s = swapcase("AbC") --> "aBc"Return true if string starts with prefix.
Parameters:
s(string): Input string.prefix(string|string[]): Prefix string.start?(integer): Optional start index (defaults to1).stop?(integer): Optional exclusive end index (defaults to#s + 1).
Return:
ok(boolean): True whensstarts withprefix.
Example:
ok = startswith("hello.lua", "he") --> trueNote
If prefix is a list, returns true when any prefix matches.
Return titlecased copy.
Parameters:
s(string): Input string.
Return:
s(string): Computed string value.
Example:
s = title("hello world") --> "Hello World"Translate characters using a mapping table.
Parameters:
s(string): Input string.table_map(table): Character translation map.
Return:
s(string): Computed string value.
Example:
map = { [string.byte("a")] = "b", ["c"] = false }
s = translate("abc", map) --> "bb"Return uppercased copy.
Parameters:
s(string): Input string.
Return:
s(string): Computed string value.
Example:
s = upper("Hello") --> "HELLO"Pad numeric string on the left with zeros.
Parameters:
s(string): Input string.width(integer): Target width.
Return:
s(string): Computed string value.
Example:
s = zfill("42", 5) --> "00042"