Module:String2/doc explained

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.

Functions

trim

The trim function simply trims whitespace characters from the start and end of the string.

title

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.

sentence

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:

So are lists:

ucfirst

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.

findlast

One potential issue is that using Lua special pattern characters (^$%.[]*+-?) as the separator will probably cause problems.

Examples
CaseWikitextOutput
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> || |}

split

The 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):

Usage
  • <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 <nowiki>["[</nowiki> to be passed. Use {{tl|!}} for the pipe character |.

    If the optional plain parameter is set to false / no / 0 then separator is treated as a Lua pattern. The default is plain=true, i.e. normal text matching.

    The index parameter is optional; it defaults to the first chunk of text.

    The is a convenience wrapper for the split function.

    stripZeros

    The 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"

    nowiki

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

    val2percent

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

    one2a

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

    findpagetext

    The findpagetext function returns the position of a piece of text in the wikitext source of a page. It takes up to four parameters:

    • First positional parameter or |text is the text to be searched for.
    • Optional parameter |title is the page title, defaults to the current page.
    • Optional parameter |plain is either true for a plain search (default), or false for a Lua pattern search.
    • Optional parameter |nomatch is the value returned when no match is found; default is nothing.
    Examples
  • <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>