Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Template:Str len/doc: Difference between revisions

Template page
No edit summary
use {{TemplateBox}}, uses Lua, remove redundant category and interwikis already being in Wikidata
 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{documentation subpage}}
{{Use sandbox}}
<includeonly>{{intricate template}}</includeonly>
{{Lua|Module:String}}
<!-- PLEASE ADD CATEGORIES AND INTERWIKIS AT THE BOTTOM OF THIS PAGE -->
{{TemplateBox
{{caution
  |1        = 1
| This template is brand new. It might need a little more testing by other users, but it has passed all my tests. <br> --[[User:Davidgothberg|David Göthberg]] ([[User talk:Davidgothberg|talk]]) 14:43, 28 March 2009 (UTC)
  |1label-en= string
  |1d-en    = The string to get the length of
  |1aliases = <!-- List of aliases, separated by / (a slash) -->
  |1type    = <!-- number, string, string/wiki-user-name, string/wiki-page-name, string/line; unknown -->
  |1def    = <!-- default value for the parameter -->
  |1stat    = required
 
  |name              = Str len
  |desc              = This template takes a string as parameter and returns its length (excluding whitespace at the start and end).
  |usage-notes        =
  |namespace          = template
  |usergroup          = all
  |shorthand          =
  |mustbesubst        =
  |setscats          =
  |placement          =
  |example            =
  |example-value      =
  |relieson          =
  |seealso            = {{string templates see also}}
  |print              =
  |type              =
  |i18n-method        = -
  |i18n-desc          =
  |i18n-subpage      =
  |i18n-mediawiki-msg =
  |useTemplateData    = only
}}
}}
 
== Examples ==
This is the {{tl|str len}} meta-template.
 
It takes a string as parameter and returns its length. It can count up to 500 in length, and returns 500 for strings that are too long.
 
'''Note!''' For most usage cases it is much easier and costs much less server resources to use {{tl|str ≥ len}} or one of its sister templates. See the [[#See also|see also]] section below.
 
=== Examples ===


If the parameter is empty or undefined it is considered to be of zero length:  
If the parameter is empty or undefined it is considered to be of zero length:  
Line 28: Line 47:
:<code><nowiki>{{str len| café åäö }}</nowiki></code> returns {{str len| café åäö }}
:<code><nowiki>{{str len| café åäö }}</nowiki></code> returns {{str len| café åäö }}


It can measure strings up to 500 in length, and returns 500 for strings that are too long:
It can measure strings over 500 in length:


:<code><nowiki>{{str len| 499-characters... }}</nowiki></code> returns {{str len| abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi }}
:<code><nowiki>{{str len| 499-characters... }}</nowiki></code> returns {{str len| abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi }}
:<code><nowiki>{{str len| 1000-characters... }}</nowiki></code> returns {{str len| abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghij }}
:<code><nowiki>{{str len| 1000-characters... }}</nowiki></code> returns {{str len| abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghij }}
=== Technical details ===
MediaWiki has no parser function or magic word to measure string lengths. And measuring string length using template code is very heavy on the servers. Thus this template is as optimised as possible. But you should consider using {{tl|str ≥ len}} instead, since for most usage cases it is much easier to use and it costs much less server resources.
This template calls {{tl|str len/core}} which does most of the work. For documentation on the internal workings of these two templates see /core. The [[Template talk:Str len|talk page]] might also have more details.
=== See also ===
{{string templates see also}}
<includeonly>
<!-- CATEGORIES AND INTERWIKIS HERE, THANKS -->
[[Category:String manipulation templates|{{PAGENAME}}]]
</includeonly>

Latest revision as of 16:08, 15 March 2020

This template takes a string as parameter and returns its length (excluding whitespace at the start and end).

Usage

{{Str len |1= }}

Template parameters

<templatedata>JSON</templatedata> ./. {{TemplateBox}}
TemplateData

TemplateData is a way to store information about template parameters (the description of those and of the whole template) for both humans and machines. It is used by VisualEditor and possibly other tools like Upload Wizard.


Existing template documentation
At Wikimedia Commons, it is recommended to use {{TemplateBox}} with either ‎useTemplateData=1 or ‎useTemplateData=only on the ‎/doc subpage and transcluding it with {{Documentation}} into the template. ‎<nowiki>-tags can be wrapped around the arguments, if required, to avoid templates being expanded.

Newly created template documentation and imports
Another option, especially for imported templates, or for users with JSON experience, is placing raw ‎<templatedata>-tags into the Wikitext of the template, as described in various Wikipediae.


Wikipedia's help about TemplateDataCommons-specific information

This template takes a string as parameter and returns its length (excluding whitespace at the start and end).

Template parameters

This template prefers inline formatting of parameters.

ParameterDescriptionTypeStatus
string1

The string to get the length of

Unknownrequired

Additional information

The template is intended to be used in the following namespaces: the Template namespace

The template is intended to be used by the following user groups: all users

See also

Trimming templates:

  • {{Trunc}} – To trim down to a specified number of characters.
  • {{Trim}} — To trim any leading or trailing whitespace.
  • {{Str left}} – To trim down to a specified number of characters, or duplicate the string to a specified number.
  • {{Str right}}
  • {{Strip}} – To strip (remove) a given number of characters from the beginning/end of the string

Analyzing templates:

  • {{Str endswith}} – To check if a string ends with a given string.
  • {{Str find}} – Returns the numerical location of a given string in a string, up to 50 characters.
  • {{Str len}} – Returns a string's length. Can count up to 500 characters.

String length comparison templates:

  • {{Str ≤ len}} – To check if a string is "shorter or equally long" or "longer" than a given length.

Sandboxes and test cases:

Technical stuff:

Localization

This template is not intended to be localized.

Examples

If the parameter is empty or undefined it is considered to be of zero length:

{{str len}} returns 0
{{str len|}} returns 0
{{str len| }} returns 0

It counts the length correctly, even if the string contains spaces, looks like a number, or contains special characters:

{{str len| ab cde }} returns 6
{{str len| 1234567 }} returns 7
{{str len| café åäö }} returns 8

It can measure strings over 500 in length:

{{str len| 499-characters... }} returns 499
{{str len| 1000-characters... }} returns 1000
Cookies help us deliver our services. By using our services, you agree to our use of cookies.