Content deleted Content added
new section "deleted functions" |
+Cat |
||
(29 intermediate revisions by 17 users not shown) | |||
Line 1:
{{high-use}}
{{module rating|release}}
{{Lua|Module:GetParameters}}
{{Lmd|String2}}
Line 13 ⟶ 15:
=== title ===
The title function capitalises the first letter of each word in the text, apart from a number of short words
This is a very simplistic algorithm; see [[:Template:Title case/doc]] 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
* <code><nowiki>{{#invoke:String2|sentence|[[trimix (breathing gas)|trimix]]}}</nowiki></code>
So are lists:
* <code><nowiki>{{#invoke:String2 |sentence |{{hlist ||[[apples]] |[[pears]] |[[oranges]]}}}}</nowiki></code> → {{#invoke:String2 |sentence |{{hlist |[[apples]] |[[pears]] |[[oranges]]}}}}
Line 49 ⟶ 53:
| List missing || <code><nowiki>{{#invoke:String2 |findlast |}}</nowiki></code> || {{#invoke:String2 |findlast |}}
|}
=== 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
: <code><nowiki>{{#invoke:String2 |split |text |separator |index |true/false}}</nowiki></code>
: <code><nowiki>{{#invoke:String2 |split |txt=text |sep=separator |idx=index |plain=true/false}}</nowiki></code>
Any double quotes (") in the separator parameter are stripped out, which allows spaces and wikitext like <code><nowiki>["[</nowiki></code> to be passed. Use <code>{{tl|!}}</code> for the pipe character <code>|</code>.
Line 81 ⟶ 67:
The index parameter is optional; it defaults to the first chunk of text.
The
=== 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"
=== nowiki ===
Line 101 ⟶ 87:
* 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 [[mw:Extension:Scribunto/Lua reference manual #Patterns|Lua pattern]] search.
* Optional parameter |nomatch is the value returned when no match is found; default is nothing.
Line 119 ⟶ 105:
The strip function strips the first positional parameter of the characters or pattern supplied in the second positional parameter.
; Usage
: <code><nowiki>{{#invoke:String2|strip|</nowiki><var>source_string</var>|<var>characters_to_strip</var>|<var>plain_flag</var>}}</code>
: <code><nowiki>{{#invoke:String2|strip|</nowiki>source=|chars=|plain=}}</code>
; Examples
: <code><nowiki>{{#invoke:String2|strip|abc123def|123}}</nowiki></code> → {{#invoke:String2|strip|abc123def|123}}
: <code><nowiki>{{#invoke:String2|strip|abc123def|%d+|false}}</nowiki></code> → {{#invoke:String2|strip|abc123def|%d+|false}}
: <code><nowiki>{{#invoke:String2|strip|source=abc123def|chars=123}}</nowiki></code> → {{#invoke:String2|strip|source=abc123def|chars=123}}
: <code><nowiki>{{#invoke:String2|strip|source=abc123def|chars=%d+|plain=false}}</nowiki></code> → {{#invoke:String2|strip|source=abc123def|chars=%d+|plain=false}}
=== matchAny ===
{{for|a function to replace multiple patterns|Module:MultiReplace}}
The matchAny function returns the index of the first positional parameter to match the ''source'' parameter. If the ''plain'' parameter is set to false (default true) then the search strings are Lua patterns. This can usefully be put in a switch statement to pick a switch case based on which pattern a string matches. Returns the empty string if nothing matches, for use in {{pf|if}}.
<code>{{((}}#invoke:String2|matchAny|123|abc|source=abc 124}}</code> returns 2.
===
Extracted hyphen_to_dash() function from [[Special:Permalink/1017669505|Module:Citation/CS1]].
Converts a hyphen to a dash under certain conditions. The hyphen must separate like items; unlike items are returned unmodified. These forms are modified:
* letter - letter (A - B)
* digit - digit (4-5)
* digit separator digit - digit separator digit (4.1-4.5 or 4-1-4-5)
* letterdigit - letterdigit (A1-A5) (an optional separator between letter and digit is supported – a.1-a.5 or a-1-a-5)
* digitletter - digitletter (5a - 5d) (an optional separator between letter and digit is supported – 5.a-5.d or 5-a-5-d)
Any other forms are returned unmodified.
The input string may be a comma- or semicolon-separated list. Semicolons are converted to commas.
<code><nowiki>{{</nowiki>#invoke:String2|hyphen2dash|1=1-2<nowiki>}}</nowiki></code> returns {{#invoke:String2|hyphen2dash|1=1-2}}.
<code><nowiki>{{</nowiki>#invoke:String2|hyphen2dash|1=1-2; 4–10<nowiki>}}</nowiki></code> returns {{#invoke:String2|hyphen2dash|1=1-2; 4–10}}.
[[Help:Citation Style 1#Accept-this-as-written markup|Accept-this-as-written markup]] is supported, e.g. <code><nowiki>{{</nowiki>#invoke:String2|hyphen2dash|1=((1-2)); 4–10<nowiki>}}</nowiki></code> returns {{#invoke:String2|hyphen2dash|1=((1-2)); 4–10}}.
By default, a normal space is inserted after the separating comma in lists. An optional second parameter allows to change this to a different character (i.e. a thin space or hair space).
===startswith===
A startswith function similar to {{ml|string|endswith}}. Both parameters are required, although they can be blank. Leading and trailing whitespace ''is'' counted, use named parameters to avoid this if required. Outputs "yes" for true and blank for false so may be passed directly to #if.
{{markup|padding=1px 5px|
<nowiki>{{#invoke:string2|startswith|search|se}}</nowiki>|{{#invoke:string2|startswith|search|se}}|
<nowiki>{{#invoke:string2|startswith|search|ch}}</nowiki>|{{#invoke:string2|startswith|search|ch}}}}<!-- Template:Mra -->
===isnumeric===
Implements {{tl|isnumeric}}, which tests whether a value is numeric (or at least, that it can be treated as such by the [[:mw:Extension:Scribunto/Lua_reference_manual#Language_library|Scribunto language library]]). By default, it will pass through the parameter if it's numeric and be blank if it's not. If the parameter <code>boolean=true</code> is given, then it will output <code>1</code> if numeric and <code>0</code> if not numeric.
{{markup|padding=1px 5px|
<nowiki>{{#invoke:string2|isnumeric|1,327.50}}</nowiki>|{{#invoke:string2|isnumeric|1,327.50}}|
<nowiki>{{#invoke:string2|isnumeric|9,999 (1950)}}</nowiki>|{{#invoke:string2|isnumeric|9,999 (1950)}}|
<nowiki>{{#invoke:string2|isnumeric|1,327.50|boolean=true}}</nowiki>|{{#invoke:string2|isnumeric|1,327.50|boolean=true}}|
<nowiki>{{#invoke:string2|isnumeric|9,999 (1950)|boolean=true}}</nowiki>|{{#invoke:string2|isnumeric|9,999 (1950)|boolean=true}}}}
== Usage ==
Line 173 ⟶ 204:
| <nowiki>{{#invoke:String2| ucfirst | human X chromosome }}</nowiki>
| {{#invoke:String2| ucfirst | human X chromosome}}
|-
| <nowiki>{{#invoke:String2 | ucfirst | {{#invoke:WikidataIB |getValue</nowiki><br /><nowiki>| P136 |fetchwikidata=ALL |onlysourced=no |qid=Q1396889}} }}</nowiki>
| {{#invoke:String2 | ucfirst | {{#invoke:WikidataIB |getValue |P136 |fetchwikidata=ALL |onlysourced=no |qid=Q1396889}} }}
|-
| <nowiki>{{#invoke:String2 | ucfirst | {{#invoke:WikidataIB |getValue</nowiki><br /><nowiki>| P106 |fetchwikidata=ALL |list=hlist |qid=Q453196}} }}</nowiki>
| {{#invoke:String2 | ucfirst | {{#invoke:WikidataIB |getValue |P106 |fetchwikidata=ALL |list=hlist |qid=Q453196}} }}
|-
|
Line 194 ⟶ 231:
| <nowiki>{{#invoke:String2| sentence | [[trimix (breathing gas)|trimix]] }}</nowiki>
| {{#invoke:String2| sentence | [[trimix (breathing gas)|trimix]] }}
|-
| <nowiki>{{#invoke:String2| sentence | }}</nowiki>
Line 227 ⟶ 258:
|}
===
[[Template:
* <code><nowiki>{{
* <code><nowiki>{{
* <code><nowiki>{{
Modules may return strings with | as separators like this: <code><nowiki>{{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }}</nowiki></code> → {{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }}
* <code><nowiki>{{
Lua patterns can allow splitting at classes of characters such as punctuation:
* <code><nowiki>{{
* <code><nowiki>{{
Or split on anything that isn't a letter (no is treated as false):
* <code><nowiki>{{
Named parameters force the trimming of leading and trailing spaces in the parameters and are generally clearer when used:
* <code><nowiki>{{
=== One2a ===
Line 275 ⟶ 299:
{{Case templates see also}}
Templates that implement
* {{tl|nowiki}}
* {{tl|nowiki2}}
<includeonly>{{Sandbox other||
[[Category:Modules that manipulate strings|*]]
[[Category:Template metamodules]]
}}</includeonly>
|