Template:Str left/doc
{{#ifeq:doc |doc
|
{{#ifeq:show |show
| Template:Mbox }}{{#if: | | {{#ifexist:Template:Str left | [[Category:{{#switch:Template |Template=Template |Module=Module |User=User |#default=Wikipedia}} documentation pages]] | }} }} | }}
This template is used {{#if:|{{{1}}}|in system messages}}, and on approximately 1,220,000 pages, or roughly 497959% of all pages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption and server load, any changes should be tested in the template's /sandbox or /testcases subpages, or in your own user subpage. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
{{#if:{{#ifeq:doc|sandbox|1}}{{#ifeq:doc|doc|1}}||{{#switch:Lua error: First parameter must be one of edit, move, create, upload.|sysop|templateeditor|interfaceadmin=|#default=}}}}
Usage
Gives the resultant <count> of characters creating a substring of characters from the start of the trimmed string (i.e. the substring returned will have length <count>, exclusive of leading whitespace characters, which are trimmed first before <count> is invoked).
If <count> is invalid, empty or zero, an empty string is returned. If undefined, it defaults to 1.
Limitations
- Length to 500: The maximum substring stops at 500 long, yet gives no error message. Only 500 characters can be extracted even if the <string> is longer.
- Inexpensive (uses no other templates).
Examples
{{ Str left | Lorem ipsum dolor sit amet | 10 }}
→ Lorem ipsu{{Str left| Lorem ipsum dolor sit amet | 4 }}
→ Lore, (note leading spaces trimmed!){{ Str left | Lorem ipsum dolor sit amet | 1 }}
→ L{{ Str left | Lorem ipsum dolor sit amet | 0 }}
→{{ Str left | Lorem ipsum dolor sit amet | }}
→{{ Str left | Lorem ipsum dolor sit amet }}
→ L{{ Str left | Lorem ipsum dolor sit amet | 40 }}
→ Lorem ipsum dolor sit ametLorem ipsum do{{ Str left | àçé | 2 }}
→ àç- This example is demonstrating the safety of this template with the UTF-8 encoding (this should work now) :
Examples of limitations
All the limitations of use for this template and shown below are caused by the current implementation of the {{padleft:}} parser function used in this template, which does not filter its third parameter containing the characters used for padding a string to the specified length (it currently counts incorrectly the characters to extract from the padding string, and incorrectly measures its effective length; in addition this parameter is limited to 500 bytes).
The following examples are demonstrating that this template does not interpret character entity references as the character they represent. It treats them as multiple characters and can leave them truncated.
{{ Str left | a c | 3 }}
→ a&n- The output "a c" may have been intended, but the named character entity was truncated, leaving no length available for the final "c".
{{ Str left | aBc | 3 }}
→ a&#- This should display "abc", but the named character entity was truncated, leaving no length available for the final "c".
{{ Str left | a c | 3 }}
→ a c- The result is correct, as expected.
{{ Str left | a c | 3 }}
→ a&#- This last example should be equivalent to the previous one, but it is not.
This template will erase nowiki tags without counting their length:
{{ Str left | a<nowiki/>bcd | 3 }}
→ abc- Returns three characters as expected
{{ Str left | a<nowiki>E</nowiki>bcd | 3 }}
→ abc- Returns three characters, but not aEb as might have been expected.
{{ Str left | a<nowiki/>bc | 10 }}
→ abcabcabca- Returns 10 characters as expected.
HTML comments, and the "noinclude", "includeonly" and "onlyinclude" wiki markup tags are also ignored, because they are preprocessed in template parameters, before including it and substituting parameters:
{{ Str left | a<noinclude/>bc | 3 }}
→ abc{{ Str left | a<!-- comment -->bc | 3 }}
→ abc
Note also that the specified length will cause the input string to be padded by repeating it, if this length is larger than the input string length:
{{ Str left | Abc. | 10 }}
→ Abc.Abc.Ab
Template data
{{#switch:<translate></translate> |=
|no |n |false |0 = | = |¬ = |yes |y |true |1 = yes |#default = yes
}}|{{#ifexpr:{{#if:|{{#ifeq:Template:Str left/doc|{{#switch:
|#default = | = {{#ifeq: | | Template:{{{docpage}}} | }}
}}|0|1}}|not{{#ifexpr: ( {{#ifeq:doc|doc|1|0}} or ( {{#ifeq:{{#titleparts:template:str left/doc|-1|-2}}|doc|1|0}} and {{#if:|1|0}} ) )|1|0}}}}|
|#default = Str left/doc | = {{#ifeq: | | Template:Str left/doc | Str left/doc }}
}}|<span style="" title="<translate nowrap> View this template</translate>">{{#if:|<translate> v</translate>|<translate> view</translate>}}]] · [[[:Template:Fullurl:]] <span style="" title="<translate nowrap> Discuss this template</translate>">{{#if:|<translate> d</translate>|<translate> talk</translate>}}]{{#if:|| · [{{fullurl:{{#switch:
|#default = Str left/doc | = {{#ifeq: | | Template:Str left/doc | Str left/doc }}
}}|action=edit}} <span style="" title="<translate nowrap> Edit this template</translate>">{{#if:|<translate> e</translate>|<translate> edit</translate>}}]}}{{#if:1|]}}}}}} {{#if:||<translate> This is the [[<tvar name=1>Special:MyLanguage/Help:TemplateData</tvar>|TemplateData]] documentation for this template used by [[<tvar name=2>Special:MyLanguage/VisualEditor</tvar>|VisualEditor]] and other tools.</translate>}}
Str left
{{#ifeq:doc|sandbox||{{#if:{{#ifexpr: (
{{#ifeq:doc|doc|1|0}} or ( {{#ifeq:{{#titleparts:template:str left/doc|-1|-2}}|doc|1|0}} and {{#if:|1|0}} ) )|1|}}||}}}} | #default=
{{#switch:
|=
|no |n |false |0 = | = |¬ = |yes |y |true |1 = yes |#default = yes
}}|{{#ifexpr:{{#if:|{{#ifeq:Template:Str left/doc|{{#switch:
|#default = | = {{#ifeq: | | Template:{{{docpage}}} | }}
}}|0|1}}|not{{#ifexpr: ( {{#ifeq:doc|doc|1|0}} or ( {{#ifeq:{{#titleparts:template:str left/doc|-1|-2}}|doc|1|0}} and {{#if:|1|0}} ) )|1|0}}}}|
|#default = Str left/doc | = {{#ifeq: | | Template:Str left/doc | Str left/doc }}
}}|<span style="" title="<translate nowrap> View this template</translate>">{{#if:|<translate> v</translate>|<translate> view</translate>}}]] · [[[:Template:Fullurl:]] <span style="" title="<translate nowrap> Discuss this template</translate>">{{#if:|<translate> d</translate>|<translate> talk</translate>}}]{{#if:|| · [{{fullurl:{{#switch:
|#default = Str left/doc | = {{#ifeq: | | Template:Str left/doc | Str left/doc }}
}}|action=edit}} <span style="" title="<translate nowrap> Edit this template</translate>">{{#if:|<translate> e</translate>|<translate> edit</translate>}}]}}{{#if:1|]}}}}}} {{#if:||This is the TemplateData documentation for this template used by VisualEditor and other tools.}}
Str left
{{#ifeq:doc|sandbox||{{#if:{{#ifexpr: (
{{#ifeq:doc|doc|1|0}} or ( {{#ifeq:{{#titleparts:template:str left/doc|-1|-2}}|doc|1|0}} and {{#if:|1|0}} ) )|1|}}||}}}} | #default=
Lua error: expandTemplate: template loop detected.
}} }}
A template to give the <count> substring of characters from the start of the trimmed string
Parameter | Description | Type | Status | |
---|---|---|---|---|
String | 1 | The string to be trimmed and counted | String | required |
Count | 2 | Gives the <count> substring of characters from the start of the trimmed string | Number | required |
See also
- Bugzilla:22555 (historical; need for correcting padleft/padright functions and to provide better string-handling parser functions)