Jump to content

Template:Str ≤ ≥ len/doc

From Wikibooks, open books for an open world

This is the {{str ≤ ≥ len}} meta-template.

It helps other templates check if a string is "shorter" vs. "equally long" vs. "longer" than a given length.

Note! For most usage cases it might be better to use {{str ≥ len}}. (See the see also section below.)

This template takes 3 to 5 unnamed parameters.

Here is how to check if the string "abcde" is 4 characters long:

{{str ≤ ≥ len | abcde | 4
| Shorter.
| Equal.
| Longer.
}}

Which returns this:

Longer.

Templates have a problem to handle parameter data that contains equal signs "=". But that is easily solved by using numbered parameters. Here we check if the string "ab=cde" is 100 characters long:

{{str ≤ ≥ len
| 1 = ab=cde
| 2 = 100
| 3 = Shorter, not "=".
| 4 = Equal "=".
| 5 = Longer, not "=".
}}

Which returns this:

Shorter, not "=".

It probably is a good habit to always use the numbered parameters, since you might not know beforehand exactly how the data will look that your template will feed to this meta-template.

Parameters

[edit source]

Detailed parameter explanation:

{{str ≤ ≥ len
| 1 = A string, 0-500 characters long.
| 2 = 0 - 500
| 3 = Data to return/render when "shorter than".
| 4 = Data to return/render when "equally long".
| 5 = Data to return/render when "longer than".
}}

Empty or undefined strings are considered to be of 0 length. Strings longer than 500 are not truncated to 500.

Length values below 0 are considered to be 0. Length values above 500 are considered to be 500.

Thus a string longer than 500 is always considered to be "longer", even if it is compared with the "correct" length value.

Technical details

[edit source]

Potential alternative templates names would be {{str <=> len}}, {{str >=< len}} or {{str ≥ ≤ len}}, but we can't use the first two of those template names since they don't work in MediaWiki.

Even though this is a string handling template it uses efficient code. (While some of the other string handling templates are probably so heavy on the servers that we perhaps should not use them.)

Templates have problems to handle parameter data that contains pipes "|", unless the pipe is inside another template {{name|param1}} or inside a piped link [[Help:Template|help]]. Thus templates can not handle wikitables as input unless you escape them by using the {{!}} magic word. This makes it hard to use wikitables as parameters to templates. Instead the usual solution is to use "HTML wikimarkup" for the table code, which is more robust.

See also

[edit source]

Trimming templates:

  • {{Str left}} – To trim down to a specified number of characters, or duplicate the string to a specified number.
  • {{Str trim}} — To trim any leading or trailing whitespace.
  • {{Str right}} — To trim the first specified number of characters.
  • {{Str rightc}} — To trim the first specified number of characters, returns a parameter if the string is empty.
  • {{Str sub}} — To trim down to a specified number, starting at a given number from the left, less than 50.
  • {{Str sub long}} — To trim down to a specified number, starting at a given number from the left, less than 100.

Analyzing templates:

  • {{Str find}} – Returns the numerical location of a given string in a string, up to 50 characters.
  • {{Str index}} – Returns a given character from a string, but not accented letters.
  • {{Str index any}} – Returns a given character from a string, but not a space.
  • {{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 "longer or equally long" or "shorter" than a given length.
  • {{Str ≤ len}} – To check if a string is "shorter or equally long" or "longer" than a given length.
  • {{Str ≠ len}} – To check if a string is "not equal" or "equal" to a given length.
  • {{Str ≤ ≥ len}} – To check if a string is "shorter", "equal", or "longer" than a given length.