String provides access to basic string functions. This module allows simple text manipulation and is dozens of times more efficient that its parser function counterparts. MediaWiki's native string functions have a upper limit defined by wgPFStringLengthLimit
as 1000 characters - this module lacks any such limit.
The default category name for errors is "Errors reported by Module String". However, this can be overriden if your wiki configures Module:String/category to return a different category name. On this wiki, the category is configured to Scribunto's general error category, and this configuration corresponds to "Pages with script errors" on wikis written in your user language.
The majority of template functions provided can be invoked with named parameters, unnamed parameters, or a mixture.
If named parameters are used, Mediawiki will automatically remove leading or trailing whitespace from the parameter - see str._getParameters
.
Template options:
ignore_errors
: If set totrue
or1
, any error condition will result in an empty string being returned rather than an error message.error_category
: If an error occurs, overrides the name of a category to include with the error message.no_category
: If set totrue
or1
, no category will be added if an error is generated.
Installation
Documentation
Package items
str.len(frame)
(function)- Computes the length of the target string.
- Parameters:
- Returns: Length of Unicode string. (number)
- Usage:
str.sub(frame)
(function)- Extracts a substring of the target string at specified indices. Indexing is 1-based for the target string to extract from. If either
i
orj
is a negative value, it is interpreted the same as selecting a character by counting from the end of the string. Hence, a value of-1
is the same as selecting the last character of the string. - If the requested indices are out of range for the given string, an error is reported.
- Parameters:
- Errors:
- Returns: Substring from
i
/1
toj
/#s
. - Usage:
str.sublength(frame)
(function)- Implements
{{str sub old}}
. This function is kept in order to maintain these older templates. Indexing is 0-based for the substring start position. - Parameters:
- Returns: Substring starting with
i
/0
of lengthlen
. (string) - Warning: This function is deprecated in favor of
str.sub
. str.match(frame)
(function)- Extracts a substring matching a pattern from the source string. If
match
orstart
are out of range for the string being queried, then this function generates an error. - An error is also generated if no match is found. If one adds the parameter ignore_errors=true, then the error will be suppressed and an empty string will be returned on any failure.
- Parameters:
frame
Invocation frame object. (table)frame.args
Invocation/template arguments. (table)frame.args.s
Target string to search. (string)frame.args.pattern
The pattern or string to find within the string. (string)frame.args.start
The index within the source string to start the search. The first character of the string has index 1. Default:1
. (string)frame.args.match
In some cases it may be possible to make multiple matches on a single string. This specifies which match to return, where the first match ismatch = 1
. If a negative number is specified then a match is returned counting from the last match. Hencematch = -1
is the same as requesting the last match. Default:1
. (string)frame.args.plain
A flag indicating that the pattern should be understood as a literal. Default:false
. (string)frame.args.nomatch
If no match is found, output the "nomatch" value rather than an error. (string)
- Errors:
- Returns: Substring of the source string matching a pattern or string literal. (string)
- Usage:
- See also:
str.pos(frame)
(function)- Returns a single character from the target string. Indexing is 1-based for the target string position.
- If one requests a negative value, this function will select a character by counting backwards from the end of the string. In other words,
pos = -1
is the same as asking for the last character. - A requested value of zero, or a value greater than the length of the string returns an error.
- Parameters:
- Error: 'string index out of range' (string; line 274)
- Returns: Single character at position
pos
. (string) - Usage:
str.find(frame)
(function)- Searches for a target string/pattern in a string. This function should be safe for UTF-8 strings.
- Parameters:
frame
Invocation frame object. (table)frame.args
Invocation/template arguments. (table)frame.args.source
The string to search. (string)frame.args.target
The string or pattern to find within thesource
string. (string)frame.args.start
The index within the source string to start the search. Default:1
. (string)frame.args.plain
Boolean flag indicating that target should be understood as a literal and not as a Lua style regular expression. Default:true
. (string)
- Returns: First index >=
start
/1
thattarget
is found withinsource
. Indexing is 1-based. Iftarget
is not found, then this function returns 0. If either "source" or "target" are missing / empty, this function also returns 0. - Usage:
str.str_find(frame)
(function)- Duplicates the behavior of
{{str find}}
including its quirks. This is provided in order to support older templates. - Parameters:
- Returns: The first index in
source
that is a match totarget
. Indexing is 1-based, and the function returns-1
if thetarget
string is not present insource
. - Note: If the "target" string is empty / missing, this function returns a value of
'1'
, which is generally unexpected behavior, and must be accounted for separatetly. - Warning: This function is deprecated in favour of
str.find
. str.prefix(frame)
(function)- Determines the presence of a prefix in a string.
- Parameters:
- Returns: Boolean flag indicating prefix presence. (string)
- Usage:
str.suffix(frame)
(function)- Determines the presence of a suffix in a string.
- Parameters:
- Returns: Boolean flag indicating suffix presence. (string)
- Usage:
str.count(frame)
(function)- Counts the number of occurrences of one string in another.
- Parameters:
- Returns: Number of occurences in target string. (number)
- Usage:
str.replace(frame)
(function)- Replaces a target string or pattern within another string.
- Parameters:
frame
Invocation frame object. (table)frame.args
Invocation/template arguments. (table)frame.args.source
The string to search. (string)frame.args.pattern
The string or pattern to find within the source. (string)frame.args.replace
The replacement text (string)frame.args.count
The number of occurences to replace. Defaults to all occurences. (string)frame.args.plain
Boolean flag indicating that pattern should be understood as a literal and not as a Lua style regular expression. Default:'true'
. (string)
- Usage:
str.rep(frame)
(function)- Repeats a string times. A simple template pipe for the
string.rep
Lua function. - Parameters:
- Error: 'function rep expects a number as second parameter, received $count' (string; line 513)
- Returns: String with repeated copies of
source
. (string) - Usage:
str.lc(frame)
(function)- Convert string to lowercase Unicode character sequence.
- Parameters:
- Returns: Lowercase UTF-8 string.
- Usage:
str.uc(frame)
(function)- Convert string to uppercase Unicode character sequence.
- Parameters:
- Returns: Uppercase UTF-8 string.
- Usage:
str.lcfirst(frame)
(function)- Convert string prefix to lowercase Unicode character.
- Parameters:
- Returns: UTF-8 string with lowercase prefix letter.
- Usage:
str.ucfirst(frame)
(function)- Convert string prefix to uppercase Unicode character.
- Parameters:
- Returns: UTF-8 string with uppercase prefix letter.
- Usage:
str.padleft(frame)
(function)- Pads beginning of a string with a character or whitespace.
- Parameters:
- Returns: String padded to the left. (string)
- Usage:
str.padright(frame)
(function)- Pads end of a string with a character or whitespace.
- Parameters:
- Returns: String padded to the right. (string)
- Usage:
str.explode(frame)
(function)- Return delimited string piece, like PHP's
explode()
. Indexing is 0-based to match the behavior of{{#explode}}
parser function. - Parameters:
- Returns: Percent-encoded string. (string)
- Usage:
str.urlencode(frame)
(function)- Percent-encoding for strings.
- Parameters:
- Returns: Percent-encoded string. (string)
- Usage:
str.urldecode(frame)
(function)- URL decoding for strings.
- Parameters:
- Returns: Percent-decoded string. (string)
- Usage:
str.htmlencode(frame)
(function)- Replaces characters in a string with HTML entities.
- Parameters:
- Returns: HTML-encoded string. (string)
- Usage:
str.htmldecode(frame)
(function)- Replaces HTML entities in the string with the corresponding characters.
- Parameters:
frame
Invocation frame object. (table)frame.args
Invocation/template arguments. (table)frame.args.s
Source string to decode. (string)frame.args.decodeNamedEntities
If decodeNamedEntities is omitted or false, the only named entities recognized are '<', '>', '&', and '"'. Otherwise, the list of HTML5 named entities to recognize is loaded from PHP's get_html_translation_table function. (string)
- Returns: Decoded string. (string)
- Usage:
Helper functions
str._getParameters(frame_args, arg_list)
(function)- Populates an argument list with both named/unnamed parameters. This is relevant because named parameters are not identical to unnamed parameters due to string trimming, and when dealing with strings we sometimes want to either preserve or remove that whitespace depending on the application.
- Parameters:
- Returns: Map of named arguments corresponding to
arg_list
. (table) str._error(exception, frame)
(function)- Helper function to handle error messages.
- Parameters:
- Returns: Optional error message, with or without categorisation. (string)
str._getBoolean(str)
(function)- Helper function to interpret boolean strings.
- Parameter:
str
Boolean-like wikitext string. (string) - Returns: Boolean value corresponding to
str
. (boolean) str._escapePattern(pattern_str)
(function)- Helper function that escapes all pattern characters. This allows patterns to be treated as plain text.
- Parameter:
pattern_str
Lua pattern string with special characters. (string) - Returns: Escaped Lua pattern string for literal string matches. (string)
str.main()
(function)- Wrapper function for string template.
- Usage:
{{string|function|<...>}}
See also