Zelda Wiki

Want to contribute to this wiki?
Sign up for an account, and get started!

Come join the Zelda Wiki community Discord server!

READ MORE

Zelda Wiki
Advertisement

This is the documentation page for Module:UtilsString

This module provides utility functions for manipulating Lua strings in general. For string manipulation and formatting that is specific to wikitext, see Module:UtilsMarkup.

This module re-implements some of the functions in the mw.text library. Use these functions whenever possible — mw.text is an order of magnitude slower because it uses mw.ustring.

Like Module:UtilsTable, some functions have both procedural and functional variants.

This module exports the following functions.

isEmpty

isEmpty(str)

Returns
  • true if and only if the value is nil or ""
Examples
InputOutputStatus
isEmpty(nil)
true
Green check.svg
isEmpty("")
true
Green check.svg
isEmpty(" ")
false
Green check.svg

notEmpty

notEmpty(str)

Returns
  • true if and only if str is neither nil nor an empty string.
Examples
InputOutputStatus
notEmpty(" ")
true
Green check.svg
notEmpty("")
false
Green check.svg
notEmpty(nil)
false
Green check.svg

isBlank

isBlank(str)

Returns
  • true if and only if str is nil, blank, or whitespace.
Examples
InputOutputStatus
isBlank("  ")
true
Green check.svg
isBlank("\n\n\n")
true
Green check.svg
isBlank(nil)
true
Green check.svg
isBlank("foo")
false
Green check.svg

nilIfEmpty

nilIfEmpty(str)

Returns
  • nil if value is nil or empty string, otherwise returns the given value.
Examples
InputOutputStatus
nilIfEmpty("")
nil
Green check.svg
nilIfEmpty(nil)
nil
Green check.svg
nilIfEmpty(" ")
" "
Green check.svg

startsWith

startsWith_startsWith

startsWith(str, pattern)

Returns
  • true if str starts with pattern, else false.
Examples
InputOutputStatus
startsWith("Fooloo Limpah", "Foo")
true
Green check.svg
startsWith("Fooloo Limpah", "foo")
false
Green check.svg
startsWith("Fooloo Limpah", "")
true
Green check.svg
startsWith("[[foo]]", "[[")
true
Green check.svg

_startsWith(pattern)(str)

Returns
  • true if str starts with pattern, else false.
Examples
InputOutputStatus
_startsWith("Foo")("Fooloo Limpah")
true
Green check.svg
_startsWith("foo")("Fooloo Limpah")
false
Green check.svg
_startsWith("")("Fooloo Limpah")
true
Green check.svg
_startsWith("[[")("[[foo]]")
true
Green check.svg

startsWithRegex

startsWithRegex_startsWithRegex

startsWithRegex(str, pattern)

Returns
  • true if str starts with regular expression pattern, else false.
Examples
InputOutputStatus
startsWithRegex("foo", "[af]")
true
Green check.svg
startsWithRegex("aoo", "[af]")
true
Green check.svg
startsWithRegex("boo", "[af]")
false
Green check.svg

_startsWithRegex(pattern)(str)

Returns
  • true if str starts with regular expression pattern, else false.
Examples
InputOutputStatus
_startsWithRegex("[af]")("foo")
true
Green check.svg
_startsWithRegex("[af]")("aoo")
true
Green check.svg
_startsWithRegex("[af]")("boo")
false
Green check.svg

endsWith

endsWith_endsWith

endsWith(str, pattern)

Returns
  • true if str ends with pattern, else false.
Examples
InputOutputStatus
endsWith("Fooloo Limpah", "Limpah")
true
Green check.svg
endsWith("Fooloo Limpah", "limpah")
false
Green check.svg
endsWith("Fooloo Limpah", "")
true
Green check.svg
endsWith("Wood (Character)", ")", true)
true
Green check.svg

_endsWith(pattern)(str)

Returns
  • true if str ends with pattern, else false.
Examples
InputOutputStatus
_endsWith("Limpah")("Fooloo Limpah")
true
Green check.svg
_endsWith("limpah")("Fooloo Limpah")
false
Green check.svg
_endsWith("")("Fooloo Limpah")
true
Green check.svg
_endsWith(")")("Wood (Character)")
true
Green check.svg

endsWithRegex

endsWithRegex_endsWithRegex

endsWithRegex(str, pattern)

Returns
  • true if str ends with regular expression pattern, else false.
Examples
InputOutputStatus
endsWithRegex("Wood (Character)", "%([^)]*%)")
true
Green check.svg
endsWithRegex("Wood", "%([^)]*%)")
false
Green check.svg

_endsWithRegex(pattern)(str)

Returns
  • true if str ends with regular expression pattern, else false.
Examples
InputOutputStatus
_endsWithRegex("%([^)]*%)")("Wood (Character)")
true
Green check.svg
_endsWithRegex("%([^)]*%)")("Wood")
false
Green check.svg

split

split_split

split(str, [pattern], [plain])

A performant alternative to mw.text.split.

Parameters
Returns
  • A table of the split strings.
Examples
InputOutputStatus
split(" foo,    bar,baz ")
{" foo", "bar", "baz "}
Green check.svg
split("foo bar baz", " ")
{"foo", "bar", "baz"}
Green check.svg
Limited support for Unicode strings
split("アイウエオ", "")
{"ア", "イ", "ウ", "エ", "オ"}
Green check.svg

_split([pattern], [plain])(str)

A performant alternative to mw.text.split.

Parameters
Returns
  • A table of the split strings.
Examples
InputOutputStatus
_split(nil)(" foo,    bar,baz ")
{" foo", "bar", "baz "}
Green check.svg
_split(" ")("foo bar baz")
{"foo", "bar", "baz"}
Green check.svg
Limited support for Unicode strings
_split("")("アイウエオ")
{"ア", "イ", "ウ", "エ", "オ"}
Green check.svg

sub

sub_sub

sub(str, startIndex, [endIndex])

Equivalent to string.sub.

Parameters
Returns
  • Function returning a substring of str from startIndex to endIndex (inclusive).
Examples
InputOutputStatus
sub("Fooloo Limpah", 8)
"Limpah"
Green check.svg
sub("Fooloo Limpah", 1, 6)
"Fooloo"
Green check.svg
sub("Fooloo Limpah", 20)
""
Green check.svg
sub("Fooloo Limpah", -20)
"Fooloo Limpah"
Green check.svg
sub("Fooloo Limpah", 8, 20)
"Limpah"
Green check.svg

_sub(startIndex, [endIndex])(str)

Equivalent to string.sub.

Parameters
Returns
  • Function returning a substring of str from startIndex to endIndex (inclusive).
Examples
InputOutputStatus
_sub(8)("Fooloo Limpah")
"Limpah"
Green check.svg
_sub(1, 6)("Fooloo Limpah")
"Fooloo"
Green check.svg
_sub(20)("Fooloo Limpah")
""
Green check.svg
_sub(-20)("Fooloo Limpah")
"Fooloo Limpah"
Green check.svg
_sub(8, 20)("Fooloo Limpah")
"Limpah"
Green check.svg

trim

trim_trim

trim(str, [pattern])

A performant alternative to mw.text.trim.

Parameters
Returns
  • The trimmed string.
Examples
InputOutputStatus
trim("  foo")
"foo"
Green check.svg
trim(":Category:Link", ":")
"Category:Link"
Green check.svg

_trim([pattern])(str)

A performant alternative to mw.text.trim.

Parameters
Returns
  • The trimmed string.
Examples
InputOutputStatus
_trim(nil)("  foo")
"foo"
Green check.svg
_trim(":")(":Category:Link")
"Category:Link"
Green check.svg

interpolate

interpolate(formatStr, args)

Approximation of string interpolation

Parameters
Returns
  • The formatted string.
Examples
InputOutputStatus
interpolate(
  "${wiki} is a ${franchise} encyclopedia that anyone can edit.",
  {
    wiki = "Zelda Wiki",
    franchise = "''Zelda''",
  }
)
"Zelda Wiki is a ''Zelda'' encyclopedia that anyone can edit."
Green check.svg
Advertisement