Module:String2/doc: Difference between revisions

Content deleted Content added
hyphen2dash: -temp note
+Cat
 
(17 intermediate revisions by 11 users not shown)
Line 1:
{{high-riskuse}}
{{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 recommendedlisted byin [[s:U.S. Government Printing Office Style Manual/Capitalization Rules#3.49|The U.S. Government Printing Office Style Manual §3.49 "Center and side heads"]]: {{xt|a, an, the, at, by, for, in, of, on, to, up, and, as, but, or, and nor}}.
 
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 |}}
|}
 
=== posnq ===
* posnq (position, no quotes) returns the numerical start position of the first occurrence of one piece of text ("target") inside another ("source"). UTC characters are supported.
* It returns nil by default if no match is found, or if either parameter is blank. If no match is found it can return the value of an optional "nomatch" parameter.
* It takes the text to be searched in as the first unnamed parameter (or {{para|source}}), which is trimmed.
* It takes the text to match as the second unnamed parameter (or {{para|target}}), which is trimmed and any double quotes " are stripped out. That allows spaces at the beginning or end of the match string to be included in a consistent manner.
* It can take an optional third unnamed parameter (or {{para|plain}}), which is trimmed. If it's set to false, then the search accepts Lua pattern-matching for the target, otherwise a plain search is used.
* It can take an optional fourth unnamed parameter (or {{para|nomatch}}), which is trimmed. This value is returned if no match occurs. Setting {{para|nomatch|0}} makes the output compatible with the find function in [[Module:String]].
; 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 |This is a piece of text to be searched |" ece"}}</nowiki></code> → {{#invoke:String2 |posnq |This is a piece of text to be searched |" ece"}}
: <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}}
: <code><nowiki>{{#invoke:String2 |posnq |source=This is a piece of text |target=%s |plain=true}}</nowiki></code> → {{#invoke:String2 |posnq |source=This is a piece of text |target=%s |plain=true}}
: <code><nowiki>{{#invoke:String2 |posnq |source=This is a piece of text |target=%s |plain=false}}</nowiki></code> → {{#invoke:String2 |posnq |source=This is a piece of text |target=%s |plain=false}}
: <code><nowiki>{{#invoke:String2 |posnq |source=This is a piece of text |target=ece |nomatch=0}}</nowiki></code> → {{#invoke:String2 |posnq |source=This is a piece of text |target=ece |plain=false |nomatch=0}}
: <code><nowiki>{{#invoke:String2 |posnq |source=This is a piece of text |target=xyz |nomatch=0}}</nowiki></code> → {{#invoke:String2 |posnq |source=This is a piece of text |target=xyz |nomatch=0}}
: <code><nowiki>{{#invoke:String2 |posnq |This is a piece of text |" of" |true |0}}</nowiki></code> → {{#invoke:String2 |posnq |This is a piece of text |" of" |true |0}}
: <code><nowiki>{{#invoke:String2 |posnq |This is a piece of text |" of" |true |0}}</nowiki></code> → {{#invoke:String2 |posnq |This is a piece of text |" of" |true |0}}
: <code><nowiki>{{#invoke:String2 |posnq |source=Meet at Café Nero |target=afé}}</nowiki></code> → {{#invoke:String2 |posnq |source=Meet at Café Nero |target=afé}}
 
=== 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>
*: <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 82 ⟶ 67:
The index parameter is optional; it defaults to the first chunk of text.
 
The [[Template:Stringsplit]]{{tl|string split}} is a convenience wrapper for the split function.
 
=== 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 |posnq strip|This is a piece of text to be searched abc123def|ext123}}</nowiki></code> → {{#invoke:String2 |posnq strip|This is a piece of text to be searched abc123def|ext123}}
: <code><nowiki>{{#invoke:String2 |posnq strip|This is a piece of text to be searched abc123def|%d+|entfalse}}</nowiki></code> → {{#invoke:String2 |posnq strip|This is a piece of text to be searched abc123def|%d+|entfalse}}
: <code><nowiki>{{#invoke:String2 |posnq strip|This is a piece of text to be searched source=abc123def|" pie"chars=123}}</nowiki></code> → {{#invoke:String2 |posnq strip|This is a piece of text to be searched source=abc123def|" pie"chars=123}}
: <code><nowiki>{{#invoke:String2 |posnq strip|source=This is a piece of text abc123def|targetchars=ece%d+|plain=false}}</nowiki></code> → {{#invoke:String2 |posnq strip|source=This is a piece of text abc123def|chars=%d+|targetplain=ecefalse}}
 
=== 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 | sentenceucfirst | {{#invoke:WikidataIB |getValue</nowiki><br /><nowiki>| P136 |fetchwikidata=ALL |onlysourced=no |qid=Q1396889}} }}</nowiki>
| {{#invoke:String2 | sentenceucfirst | {{#invoke:WikidataIB |getValue |P136 |fetchwikidata=ALL |onlysourced=no |qid=Q1396889}} }}
|-
| <nowiki>{{#invoke:String2 | sentenceucfirst | {{#invoke:WikidataIB |getValue</nowiki><br /><nowiki>| P106 |fetchwikidata=ALL |list=hlist |qid=Q453196}} }}</nowiki>
| {{#invoke:String2 | sentenceucfirst | {{#invoke:WikidataIB |getValue |P106 |fetchwikidata=ALL |list=hlist |qid=Q453196}} }}
|-
| &nbsp;
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:
|}
 
=== PosnqString split ===
[[Template:PosnqString split]] is a convenience wrapper for the posnqsplit function.
* <code><nowiki>{{PosnqString split |This is a piece of text to be searchedsplit |piece of" "}}</nowiki></code> → {{PosnqString split |This is a piece of text to be searchedsplit |piece of" "}}
* <code><nowiki>{{PosnqString split |This is a piece oftextof text to be searchedsplit |piece" of"| 4}}</nowiki></code> → {{PosnqString split |This is a piece oftextof text to be searchedsplit |piece" of"| 4}}
* <code><nowiki>{{PosnqString split |This is a piece of text to be searchedsplit |"piece ofx| "2}}</nowiki></code> → {{PosnqString split |This is a piece of text to be searchedsplit |"piece ofx| "2}}
* <code><nowiki>{{Posnq |This is a piece oftext to be searched |"piece of "}}</nowiki></code> → {{Posnq |This is a piece oftext to be searched |"piece of "}}
 
=== Stringsplit ===
[[Template:Stringsplit]] is a convenience wrapper for the split function.
* <code><nowiki>{{Stringsplit |This is a piece of text to be split |" "}}</nowiki></code> → {{Stringsplit |This is a piece of text to be split |" "}}
* <code><nowiki>{{Stringsplit |This is a piece of text to be split |" "| 4}}</nowiki></code> → {{Stringsplit |This is a piece of text to be split |" "| 4}}
* <code><nowiki>{{Stringsplit |This is a piece of text to be split |x| 2}}</nowiki></code> → {{Stringsplit |This is a piece of text to be split |x| 2}}
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>{{StringsplitString split |{{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }}|{{!}}| 2}}</nowiki></code> → {{StringsplitString split |{{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }}|{{!}}| 2}}
Lua patterns can allow splitting at classes of characters such as punctuation:
* <code><nowiki>{{StringsplitString split |Apples, pears, oranges; Cats, dogs|"%p"| 2 |false}}</nowiki></code> → {{StringsplitString split |Apples, pears, oranges; Cats, dogs|"%p"| 2 |false}}
* <code><nowiki>{{StringsplitString split |Apples, pears, oranges; Cats, dogs|"%p"| 4 |false}}</nowiki></code> → {{StringsplitString split |Apples, pears, oranges; Cats, dogs|"%p"| 4 |false}}
Or split on anything that isn't a letter (no is treated as false):
* <code><nowiki>{{StringsplitString split |Apples pears oranges; Cats dogs|"%A+"| 4 |no}}</nowiki></code> → {{StringsplitString split |Apples pears oranges; Cats dogs|"%A+"| 4 |no}}
Named parameters force the trimming of leading and trailing spaces in the parameters and are generally clearer when used:
* <code><nowiki>{{StringsplitString split | txt=Apples pears oranges; Cats dogs | sep="%A+" | idx=3 | plain=false }}</nowiki></code> → {{StringsplitString split | txt=Apples pears oranges; Cats dogs | sep="%A+" | idx=3 | plain=false }}
 
=== One2a ===
Line 295 ⟶ 299:
{{Case templates see also}}
 
Templates that implement <code><{{tag|nowiki></code>|o}}
* {{tl|nowiki}}
* {{tl|nowiki2}}
<includeonly>{{Sandbox other||
[[Category:Modules that manipulate strings|*]]
[[Category:Template metamodules]]
}}</includeonly>