Content deleted Content added
Matthiaspaul (talk | contribs) →hyphen2dash: -temp note |
+Cat |
||
(17 intermediate revisions by 11 users not shown) | |||
Line 1:
{{high-
{{module rating|release}}
{{Lua|Module:GetParameters}}
{{Lmd|String2}}
Line 14 ⟶ 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 ===
Line 50 ⟶ 53:
| List missing || <code><nowiki>{{#invoke:String2 |findlast |}}</nowiki></code> || {{#invoke:String2 |findlast |}}
|}
; Examples▼
: <code><nowiki>{{#invoke:String2 |posnq |This is a piece of text to be searched |ext}}</nowiki></code> → {{#invoke:String2 |posnq |This is a piece of text to be searched |ext}}▼
: <code><nowiki>{{#invoke:String2 |posnq |This is a piece of text to be searched |ent}}</nowiki></code> → {{#invoke:String2 |posnq |This is a piece of text to be searched |ent}}▼
: <code><nowiki>{{#invoke:String2 |posnq |This is a piece of text to be searched |" pie"}}</nowiki></code> → {{#invoke:String2 |posnq |This is a piece of text to be searched |" pie"}}▼
: <code><nowiki>{{#invoke:String2 |posnq |source=This is a piece of text |target=ece}}</nowiki></code> → {{#invoke:String2 |posnq |source=This is a piece of text |target=ece}}▼
=== 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):
* <code><nowiki>{{#invoke:String2 |split |text |separator |index |true/false}}</nowiki></code>▼
; Usage
* <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 82 ⟶ 67:
The index parameter is optional; it defaults to the first chunk of text.
The
=== stripZeros ===
Line 102 ⟶ 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 120 ⟶ 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
▲: <code><nowiki>{{#invoke:String2
▲: <code><nowiki>{{#invoke:String2
▲: <code><nowiki>{{#invoke:String2
=== 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.
Line 149 ⟶ 144:
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 193 ⟶ 204:
| <nowiki>{{#invoke:String2| ucfirst | human X chromosome }}</nowiki>
| {{#invoke:String2| ucfirst | human X chromosome}}
|-▼
| <nowiki>{{#invoke:String2 |
| {{#invoke:String2 |
|-▼
| <nowiki>{{#invoke:String2 |
| {{#invoke:String2 |
|-
|
Line 214 ⟶ 231:
| <nowiki>{{#invoke:String2| sentence | [[trimix (breathing gas)|trimix]] }}</nowiki>
| {{#invoke:String2| sentence | [[trimix (breathing gas)|trimix]] }}
▲|-
▲| <nowiki>{{#invoke:String2 | sentence | {{#invoke:WikidataIB |getValue</nowiki><br /><nowiki>| P136 |fetchwikidata=ALL |onlysourced=no |qid=Q1396889}} }}</nowiki>
▲| {{#invoke:String2 | sentence | {{#invoke:WikidataIB |getValue |P136 |fetchwikidata=ALL |onlysourced=no |qid=Q1396889}} }}
▲|-
▲| <nowiki>{{#invoke:String2 | sentence | {{#invoke:WikidataIB |getValue</nowiki><br /><nowiki>| P106 |fetchwikidata=ALL |list=hlist |qid=Q453196}} }}</nowiki>
▲| {{#invoke:String2 | sentence | {{#invoke:WikidataIB |getValue |P106 |fetchwikidata=ALL |list=hlist |qid=Q453196}} }}
|-
| <nowiki>{{#invoke:String2| sentence | }}</nowiki>
Line 247 ⟶ 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 295 ⟶ 299:
{{Case templates see also}}
Templates that implement
* {{tl|nowiki}}
* {{tl|nowiki2}}
<includeonly>{{Sandbox other||
[[Category:Modules that manipulate strings|*]]
[[Category:Template metamodules]]
}}</includeonly>
|