Help:String functions

Extension
Various string functions are part of Extension:ParserFunctions. However, this part is disabled on Wikimedia wikis, with. See also 6455 and 19298. The following description is based on these functions being DISABLED.

Concatenation
Concatenation is done by juxtaposition.

The expansion of the concatenation of two balanced wikitexts is equal to the concatenation of the two expanded wikitexts.

However, the rendering of e.g. and  does not look the same as that of the concatenated wikitext.

Trimming
Trimming is the removal of newlines and spaces from the start and the end of a string.

Automatically trimmed are:
 * the value of a named template parameter
 * the value of a parameter of a parser function (see mw:Help:Magic words and mw:Help:Extension:ParserFunctions), including the one after the colon

Thus, if trimming is desired, this is in many cases conveniently performed together with another operation one wants to perform. To just trim string S, one applies a template or parser function such that this does not affect the string, apart from the trimming, e.g.:
 * (using Template:1x containing )

Equality
For equality there are  and. To force a string-based comparison with, add a non-numerical character to both compared arguments.

Note that this prevents the trimming at the side at which the character is added. If desired, trim the arguments first. If trimming is not desired, put a non-numerical character before and after both strings, e.g. put them in quotes.

Case
See mw:Help:Magic words.

Padleft
The following applies to trimmed strings S and P, and n = 0, 1, 2,.. Note that the result is not trimmed: if S is empty the result can end with spaces and/or newlines.

pads a given string S on the left with a padding string P (default: "0"), to increase the length (where a newline counts as one character) to min(n,500), or if length (S) ≥ min(n,500) it just returns S. The padding string P is zero or more times repeated, with finally possibly a truncated P. If P is empty, S is returned.

Properties:
 * length = if P is non-empty then max ( min(n,500), length (S) ) else length (S)
 * is equal to S if and only if length (S) ≥ min(n,500) or P is empty.
 * is equal to P if and only if [length (P) = max ( min(n,500), length (S) ) or 0] and S is equal to a substring of P ending at the end of P (i.e., P is the concatenation RS of some string R with S).

An important special case is that where S is empty:  produces a string of length min(n,500) consisting of zero or more repetitions of P, with finally possibly a truncated P. In particular, if P has a length greater than min(n,500), this is the truncation of P to min(n,500) characters, i.e., the substring of P of min(n,500) characters starting at the start of P.

The above-mentioned properties reduce to:


 * length = if P is non-empty then min(n,500) else 0
 * is equal to P if and only if length (P) = min(n,500)

Examples:
 * (truncation)
 * (truncation)


 * gives "000" (the parameter is trimmed, the result is not)








 * gives 00 (hour; not needed for the current time, because there is also the variable ; see also the #time function)

Maximum string length
If characters of P are in the result (P is not empty and the string is not already longer than the required length and not longer than 500) the maximum length of the resulting string is 500:

However, if no characters of P are in the result (P is empty or the string is already longer than the required length or longer than 500), the whole string S is returned, even if it is longer:



Applications with respect to a page
Rendering the truncated expanded wikitext of a page:

gives "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

Note that when specifying an arbitrary number of characters. links, table structure, XML-style tags etc. may be broken. However, if the braces are balanced in the source page there are neither unbalanced braces in the result, because expansion is done before truncation.

Displaying the truncated expanded wikitext:

gives ""

Displaying the truncated wikitext:

gives "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

gives "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

The page has been prepared with  tags, so that the small maximum of 500 characters is not wasted on a header box or table code. This also reduces the size of what is transcluded before applying padleft, reducing the post-expand include size, for which there is a limit of 2048000 bytes per page. A disadvantage could be that other pages cannot include the rest of the page either.

Padright
The following applies to trimmed strings S and P, and n = 0, 1, 2,.. Note that the result is not trimmed.

pads a given string S on the right with a padding string P (default: "0"), to increase the length to n, or if length (S) ≥ n it just returns S. The padding string P is zero or more times repeated, with finally possibly a truncated P. If P is empty, S is returned.

Properties:
 * length = if P is non-empty then max ( n, length (S) ) else length (S)
 * is equal to S if and only if length (S) ≥ n or P is empty.

The special case where S is empty is the same as for padleft.

Examples:
 * (truncation)
 * (truncation)






 * gives the wikitext  rendered as "abc0000000000"
 * gives the wikitext  rendered as "abc00000000000"

Length of a string
There is no function for the length of a string, but from the above follows:
 * is equal to S if and only if length (S) ≥ min(n,500).

(Note that  may end with spaces and/or newlines, so care should be taken that it is not trimmed before the comparison.)

Thus with a binary search the length can be found, except that if it is 500 or more this fact can be found, not the actual length. This is done in Template:Len with the help of Template:Len/digit. They use quotation marks around the string, so the maximum length found for the string itself is 498.

Extracting a character
As follows from the above, the first character of a string P can be extracted with. This method is preferable for this case compared with the method below, for efficiency and because there are less limitations.

There is no function for extracting a character from an arbitrary given position of a string. However, for a given character we can compare the truncation of the string up to and including the given position with the truncation of the string until that position, concatenated with the character. Thus we determine whether the character at the given position in the string is equal to the character we tried. (Note that the truncation of the string until the given position may end with spaces and/or newlines, so care should be taken it is not trimmed before the concatenation. Similarly, when trying whether the character is a space or newline, care should be taken that the compared strings are not trimmed before the comparison, because then we would not distinguish between a space and a newline.) Also care should be taken that the result, which may be a space or a newline, is not unintentionally trimmed.

This is done in Template:Chr with the help of Template:Chr/list. The latter contains a switch with a case for each of the supported characters.

The automatic newline feature/bug for "{|" does not affect the result, except that it adds the newline if "{|" is at the start of the substring, since Sub calls Chr for every character position separately. However, just "*", "#", ":", and ";" without newline cannot be produced by any template or parser function. Possible remedies:
 * put the character in &lt;nowiki> tags
 * Postpone adding a resulting character to the output until the next charaxter is checked; output it together with that character is the second character is "*", "#", ":", or ";". This seems rather complex, since there is no feature for variables in the computer programming sense; it may require deep nesting, but then one may be restricted by the system limitations on the number of levels.
 * Post-process the resulting string, removing the added newlines. See Template:Rmanl for a start.

Extracting a substring
As mentioned above, if n is not greater than the length of a string P,  produces the truncation of P to n characters, i.e., the substring of P of n characters starting at the start of P. This method is preferable for this case compared with the method below, for efficiency and because there are less limitations.

There is no function for extracting a substring that does not start at the start of the string, neither one for looping. However, for every potential position we can determine whether it is within the range of the required substring, and if so, extract the character. The results are simply concatenated.

This is done in Template:Sub. Note that for each of the required character positions it requires two calls of padleft and a call of a switch. Thus quickly system limits are reached.

Extracting a substring from the right side of a given string
Extracting a substring from some given position to the end of a string requires the specification or determination of the length of the string. This also applies to extracting a substring of a given length from the end of the string. Of course, if the length is known it is more efficient to specify it than to have the system determine it.

If the length is not known, Template:Str right and Template:Str rightc can be used, respectively, otherwise the previous section can be applied directly.

XML-style tags
XML-style tags, e.g.  tags and   tags, together with their content, are temporarily replaced by a so-called strip marker, a unique code with a length of ca. 37 characters plus the length of the tag name (independent of the length of the content), e.g.

This affects string functions. If a strip marker is truncated, the remainder is exposed. Also, padding a string with a strip marker is based on the length of that, not of what it represents.

Examples with  tags:


 * gives 00000000000000000000000000000000000000
 * gives 0000000000000000000000000000000000000000
 * gives 00000000000000000000000000000000000000000
 * gives 000000000000000000000000000000000000000000
 * gives 0000000000000000000000000000000000000000000
 * gives 000000000000000000000000000000000000000000000000


 * gives 0000000000000000000000000000000000000abc
 * gives 0000000000000000000000000000000000000000abc
 * gives 00000000000000000000000000000000000000000abc
 * gives 00000000000000000000000000000000000000000000000abc
 * gives abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc

Examples with  tags:


 * gives for example 00000000000000000000000000000000000000
 * gives for example 0000000000000000000000000000000000000000
 * gives 0000000000000000000000000000000000000000000
 * gives 000000000000000000000000000000000000000000000000


 * gives 000000000000000000000000000000000000$abc$
 * gives 0000000000000000000000000000000000000$abc$
 * gives 0000000000000000000000000000000000000000000$abc$
 * gives $abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc$

#titleparts
See Help:Titleparts.

String storage
Suppose we have the string ABCD and also need the substring CD, then because of the inefficiency of finding the substring we can better store (or pass on as parameter values) AB and CD as separate data items.

Date and time variables are available providing output in the form of numbers, allowing easy processing without expensive string operations.

However, for processing the result of (on the page itself or passed on to a template as parameter value) string functions can be useful, to extract the text after "Wikiproject ", the text between parentheses, the text before the parentheses, etc.