The module String2 contains a number of string manipulation functions that are much less commonly used than those in . Because Module:String is cascade-protected (some of its functions are used on the Main Page), it cannot be edited or maintained by template editors, only by admins. While it is true that string-handling functions rarely need maintenance, it is useful to allow that by template editors where possible, so this module may be used by template editors to develop novel functionality.
The module contains three case-related calls that convert strings to first letter uppercase, sentence case or title case and two calls that are useful for working with substrings. There are other utility calls that strip leading zeros from padded numbers and transform text so that it is not interpreted as wikitext, and several other calls that solve specific problems for template developers such as finding the position of a piece of text on a given page.
The functions are designed with the possibility of working with text returned from Wikidata in mind. However, a call to Wikidata may return empty, so the functions should generally fail gracefully if supplied with a missing or blank input parameter, rather than throwing an error.
The trim function simply trims whitespace characters from the start and end of the string.
The title function capitalises the first letter of each word in the text, apart from a number of short words listed in The U.S. Government Printing Office Style Manual §3.49 "Center and side heads": .
This is a very simplistic algorithm; see for some of its limitations.
The sentence function finds the first letter and capitalises it, then renders the rest of the text in lower case. It works properly with text containing wiki markup. Compare <nowiki>{{#invoke:String2|sentence|[[action game]]}}</nowiki>
→ with <nowiki>{{ucfirst:{{lc:[[action game]]}}}}</nowiki>
→ . Piped wiki-links are handled as well:
<nowiki>{{#invoke:String2|sentence|[[trimix (breathing gas)|trimix]]}}</nowiki>
→So are lists:
<nowiki>{{#invoke:String2 |sentence |{{hlist ||[[apples]] |[[pears]] |[[oranges]]}}}}</nowiki>
→The ucfirst function is similar to sentence; it renders the first alphabetical character in upper case, but leaves the capitalisation of the rest of the text unaltered. This is useful if the text contains proper nouns, but it will not regularise sentences that are ALLCAPS, for example. It also works with text containing piped wiki-links and with html lists.
One potential issue is that using Lua special pattern characters (^$%.[]*+-?
) as the separator will probably cause problems.
Case | Wikitext | Output |
---|---|---|
Normal usage | <nowiki>{{#invoke:String2 |findlast | 5, 932, 992,532, 6,074,702, 6,145,291}}</nowiki> || |-| Space as separator || <nowiki>{{#invoke:String2 |findlast | 5 932 992,532 6,074,702 6,145,291 }}</nowiki> | |
One item list | <nowiki>{{#invoke:String2 |findlast | 6,074,702 }}</nowiki> | |
Separator not found | <nowiki>{{#invoke:String2 |findlast | 5, 932, 992,532, 6,074,702, 6,145,291 |;}}</nowiki> | |
List missing | <nowiki>{{#invoke:String2 |findlast |}}</nowiki> || |}splitThe split function splits text at boundaries specified by separator and returns the chunk for the index idx (starting at 1). It can use positional parameters or named parameters (but these should not be mixed):
<nowiki>{{#invoke:String2 |split |text |separator |index |true/false}}</nowiki>
<nowiki>{{#invoke:String2 |split |txt=text |sep=separator |idx=index |plain=true/false}}</nowiki> Any double quotes (") in the separator parameter are stripped out, which allows spaces and wikitext like If the optional plain parameter is set to The index parameter is optional; it defaults to the first chunk of text. The is a convenience wrapper for the split function. stripZerosThe stripZeros functions finds the first number in a string of text and strips leading zeros, but retains a zero which is followed by a decimal point. For example: "0940" → "940"; "Year: 0023" → "Year: 23"; "00.12" → "0.12" nowikiThe nowiki function ensures that a string of text is treated by the MediaWiki software as just a string, not code. It trims leading and trailing whitespace. val2percentThe val2percent functions scans through a string, passed as either the first unnamed parameter or |txt=, and converts each number it finds into a percentage, then returns the resulting string. one2aThe one2a function scans through a string, passed as either the first unnamed parameter or |txt=, and converts each occurrence of 'one ' into either 'a ' or 'an ', then returns the resultant string. The is a convenience wrapper for the one2a function. findpagetextThe findpagetext function returns the position of a piece of text in the wikitext source of a page. It takes up to four parameters:
<nowiki>{{#invoke:String2 |findpagetext |text=Youghiogheny}}</nowiki> →
<nowiki>{{#invoke:String2 |findpagetext |text=Youghiogheny |nomatch=not found}}</nowiki> →
<nowiki>{{#invoke:String2 |findpagetext |text=Youghiogheny |title=Boston Bridge |nomatch=not found}}</nowiki> →
<nowiki>{{#invoke:String2 |findpagetext |text=river |title=Boston Bridge |nomatch=not found}}</nowiki> →
<nowiki>{{#invoke:String2 |findpagetext |text=[Rr]iver |title=Boston Bridge |plain=false |nomatch=not found}}</nowiki> →
<nowiki>{{#invoke:String2 |findpagetext |text=%[%[ |title=Boston Bridge |plain=f |nomatch=not found}}</nowiki> → |