From Test-Scratch-Wiki
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.
|