}}
{{Str left|<string>|<count>}}
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.
- 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).
{{ 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) :
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 parameters[Edit template data]
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 |
---|
- Bugzilla:22555 (historical; need for correcting padleft/padright functions and to provide better string-handling parser functions)
String-handling templates (help page) |
---|
Substring (select chunk of string by its position) |
---|
{{#invoke:string|sub}} {{str sub new}} | Select a substring based on starting and ending index counted in either direction. | {{str left}} | Keep the first N characters of a string, or duplicate it to N characters. | {{str crop}} | Return all but the last N characters of a string. | {{str index}} | Return the N-th character of a string. |
| {{str right}} | Return substring starting at N-th character to end of string. | {{str rightc}} | Return the last N characters of a string; or a given value if empty. | {{str sub old}} | Return substring of given length starting at N-th character. |
|
|
|
Trimming (select/remove chunk of string by character type) |
---|
|
|
Select/remove specific item from string |
---|
| {{ship prefix}} | Returns the length of a ship's name prefix, if listed (ship names). | {{title year}} | Returns the 3-or-4-digit year from a pagename, if any. | {{title decade}} | Returns the 4-digit decade (e.g. "1740s") from a pagename, if any. | {{title number}} | Returns any number from a pagename. |
|
|
|
Insertion, replacement and length |
---|
{{loop}} | Repeat character string a specified number of times. | {{replace}} | Returns the string, after replacing all occurrences of a specified string with another string. | {{str rep}} | Returns the string, after replacing the first occurrence of a specified string with another string. | {{digits}} | Returns the string, after removing all characters that are not digits 0–9 | {{Nowiki}} | Displays wikitext as plain text. |
|
{{str len}} | Returns a string's length. | {{str ≥ len}} | Check if a string is "longer or equally long" or "shorter" than a given length. | {{str ≤ len}} | Check if a string is "shorter or equally long" or "longer" than a given length. | {{str ≠ len}} | Check if a string is "not equal" or "equal" to a given length. |
|
|
|
Analysis and searching |
---|
{{#invoke:string2|startswith}} {{str endswith}} | Check if a string starts or ends with a given string. | {{str find}}, {{strfind short}} | Returns the numerical location of a given string in a string. | {{in string}} | Returns optional string when given string is not found | {{str count}} | Count the number of occurrences of a pattern within a string. |
|
{{a or an}} | Takes a noun and produces the appropriate indefinite article, based on whether the first letter is a vowel. | {{Isnumeric}} | Returns number when true, blank when not numeric. | {{str letter}} | Returns the number of letters that begin a string. | {{str number}} | Returns the number of numbers that begin a string. |
|
|
|
Modules and built-in functions |
---|
|
|
|