If you find this page useful and would like to be notified of changes made to this page, start by inputting your email below.
powered by ChangeDetection
(+cat lua metamodules) |
m (1 revision) |
(No difference)
|
This Lua module is used on 1,400,000 pages. To avoid large-scale disruption and unnecessary server load, any changes to this module should first be tested in its /sandbox or /testcases subpages. The tested changes can then be added to this page in one single edit. Please consider discussing any changes on the talk page before implementing them. |
Module:string (talk⧼dot-separator⧽edit⧼dot-separator⧽hist⧼dot-separator⧽links⧼dot-separator⧽doc⧼dot-separator⧽subpages⧼dot-separator⧽tests – results)
This module is intended to provide access to basic string functions.
Most of the functions provided here can be invoked with named parameters, unnamed parameters, or a mixture. If named parameters are used, Mediawiki will automatically remove any leading or trailing whitespace from the parameter. Depending on the intended use, it may be advantageous to either preserve or remove such whitespace.
Unit tests for this module are available at Module:String/testcases.
This function returns the length of the target string.
Usage:
OR
Parameters:
Usage:
OR
Parameters:
The first character of the string is assigned an index of 1. If either i or j 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.
This function implements the features of {{Str sub old}} and is kept in order to maintain these older templates.
This function returns a substring from the source string that matches a specified pattern.
Usage:
OR
Parameters:
If the match_number or start_index 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.
For information on constructing Lua patterns, a form of regular expression, see:
This function returns a single character from the target string at position pos.
Usage:
OR
Parameters:
The first character has an index value of 1.
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.
This function duplicates the behavior of {{str_find}}, including all of its quirks. This is provided in order to support existing templates, but is NOT RECOMMENDED for new code and templates. New code is recommended to use the "find" function instead.
Returns the first index in "source" that is a match to "target". Indexing is 1-based, and the function returns -1 if the "target" string is not present in "source".
Important 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.
This function allows one to search for a target string or pattern within another string.
Usage:
OR
Parameters:
This function returns the first index >= "start" where "target" can be found within "source". Indices are 1-based. If "target" is not found, then this function returns 0. If either "source" or "target" are missing / empty, this function also returns 0.
This function should be safe for UTF-8 strings.
This function allows one to replace a target string or pattern within another string.
Usage:
OR
Parameters:
Repeats a string n times. A simple function to pipe string.rep to templates. Usage:
Parameters:
Example {{#invoke:String|rep|hello|3}}
gives hellohellohello