Help:String functions

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search

Extension[edit]

Various string functions are part of Extension:ParserFunctions.[1] However, this part is disabled on Wikimedia wikis, with $wgPFEnableStringFunctions = false. See also bugzilla:6455 and bugzilla:19298. The following description is based on these functions being DISABLED.

Concatenation[edit]

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. '''bo and ld''' does not look the same as that of the concatenated wikitext.

Trimming[edit]

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

Automatically trimmed are:

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.:

  • {{1x|1=S}} (using Template:1x containing {{{1}}})
  • {{#if:x|S}}
  • {{#switch:|S}}
  • {{padleft:S}}
  • {{padright:S}}

Equality[edit]

For equality there are #ifeq: and #if:. To force a string-based comparison with #ifeq:, 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[edit]

See mw:Help:Magic words#Formatting.

Padleft[edit]

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.

{{padleft:S|n|P}} 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 ( {{padleft:S|n|P}} ) = if P is non-empty then max ( min(n,500), length (S) ) else length (S)
  • {{padleft:S|n|P}} is equal to S if and only if length (S) ≥ min(n,500) or P is empty.
  • {{padleft:S|n|P}} 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: {{padleft:|n|P}} 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 ( {{padleft:|n|P}} ) = if P is non-empty then min(n,500) else 0
  • {{padleft:|n|P}} is equal to P if and only if length (P) = min(n,500)

Examples:

  • "{{padleft:|0|ab}}" gives "" [1]
  • "{{padleft:|1|ab}}" gives "a" [2] (truncation)
  • "{{padleft:|2|ab}}" gives "ab" [3]
  • "{{padleft:|3|ab}}" gives "aba" [4]
  • "{{padleft:|4|ab}}" gives "abab" [5]
  • "{{padleft:|5|ab}}" gives "ababa" [6]
  • "{{padleft:|3| a  b }}" gives "a " (the parameter is trimmed, the result is not)
  • "{{padleft:1|0|ab}}" gives "1" [7]
  • "{{padleft:1|1|ab}}" gives "1" [8]
  • "{{padleft:1|2|ab}}" gives "a1" [9]
  • "{{padleft:1|3|ab}}" gives "ab1" [10]
  • "{{padleft:1|4|ab}}" gives "aba1" [11]
  • "{{padleft:1|5}}" → "00001" [12]
  • "{{padleft:1|5|}}" gives "1" [13]
  • "{{padleft:1|5| }}" gives "1" [14]

"{{padleft:a

b|5}}"
gives "0a

b" [15]

  • "{{padleft:é|5}}" → "0000é" [16]
  • {{padleft:|2|14:38}} gives 14 (hour; not needed for the current time, because there is also the variable {{CURRENTHOUR}}; see also the #time function)

Maximum string length[edit]

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:

  • "{{padleft:abc|507|12345678 0}}" gives "12345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 01234567abc" [17]
  • "{{padleft:|507|123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123}}" gives "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 " [18]

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:

  • "{{padleft:123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123|507|}}" gives "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123" [19]
  • "{{padleft:123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123|507|p}}" gives "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123" [20]

Applications with respect to a page[edit]

Rendering the truncated expanded wikitext of a page:

{{padleft:|500|{{Help:Parser function}}}} gives "A parser function is one of the double-brace structures that can be in a page, see also Help:Expansion. It returns a value based on at least one unnamed parameter, separated from the function name by a colon ":". There may also be additional parameters, which, like those of ordinary templates, are each preceded by "|". Parser functions differ by whether they have a leading hash character (#):

  1. functionname: argument 1 | argument 2 | argume"

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:

{{#tag:nowiki|{{padleft:|500|{{Help:Parser function}}}}}} gives "A '''parser function''' is one of the double-brace structures that can be in a page, see also [[Help:Expansion]]. It returns a value based on at least one unnamed parameter, separated from the function name by a colon "''':'''". There may also be additional parameters, which, like those of ordinary templates, are each preceded by "'''|'''". Parser functions differ by whether they have a leading [[w:Number sign|hash character]] (#): # ''functionname'': ''argument 1'' | ''argument 2'' | ''argume"

Displaying the truncated wikitext:

{{padleft:|500|{{msgnw:Help:Parser function}}}} gives "{{h:h|editor toc}} {{languages}} {{otheruses4|parser functions in general|the MediaWiki extension '''ParserFunctions'''|mw:Help:Extension:ParserFunctions}} <onlyinclude>A '''parser function''' is one of the double-brace structures that can be in a page, see also [[Help:Expansion]]. It returns a value based on at least one unnamed parameter, "

{{padleft:|500|{{Help:Template}}}} gives "