Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. (December 2019) |
The StringFunctions extension adds a set of parser functions that allows more editing capabilities to strings. The syntax of parser functions are: {{#functionname:argument 1|argument 2|argument 3 ...}}}
. All of these functions are safe with UTF-8 multibyte characters.
#len
The #len (length) function accepts a string and outputs the length of the string. Leading spaces are not counted, but intermediate spaces and newlines are counted. Entities are counted according to their source form, however. Some wiki tags, such as nowiki, are not counted.
Code | Output | Explanation |
---|---|---|
{{#len:rsgd}}
|
4 | The length of the string is 4. |
{{#len:-}}
|
5 | Entities are converted after the parser function runs. |
{{#len:hi }}
|
2 | Trailing space is not counted. |
{{#len:hi bye}}
|
10 | Intermediate spaces are counted. |
#pos
The #pos function finds a term in a given string, and returns its position. If the search term is immediately found in a string, it returns "0". An optional parameter, offset, tells the function in what location of the string it should begin searching at. If the search term does not exist in the given string, it returns nothing. The syntax for this function is {{#pos:string|search term|offset}}
.
Code | Output | Explanation |
---|---|---|
{{#pos:shrkana|na}}
|
5 | "na" appears in "shrkana" at position 5. |
{{#pos:apgkspamb|a|2}}
|
6 | "a" appears at position 0, but since the offset it 2, the next "a" that appears after position 2 is in location 6. |
{{#pos:<span>sa</span>da|da}}
|
15 | Some wiki tags count in the final result. Others, such as the <nowiki> tag, are only counted as one character.
|
#replace
This function returns a string with all instances of the search string replaced with the replacement string.
{{#replace:string|search string|replacement string}}
If the search string is unspecified or empty, a single space will be searched for.
If the replacement string is unspecified or empty, all occurrences of the search term will be removed from the string.
Code | Output | Explanation |
---|---|---|
{{#replace:I have no friends.|no|a lot of}}
|
I have a lot of friends. | "no" is replaced with "a lot of". |
{{#replace:THIS IS CAPITALIZED|capitalized|now lowercase}}
|
THIS IS CAPITALIZED | This function is case-sensetive. |
{{#replace:My_little_home_page|_| }}
|
Mylittlehomepage | Even if the replacement term is a space, an empty string is used. This is a side-effect of the MediaWiki parser. |
{{#replace:My_little_home_page|_| }}
|
My little home page | Use a Non-breaking space if you want to pass spaces to this function. |