Template documentationview · edit · history · purge ]
This documentation is transcluded from Template:Char/doc.

The Template:Char extracts a specified nth character from a string, even allowing for accented letters to be included in the string.

Usage edit

{{Char|text|n}} = Returns the n-th character of text.

Supports a larger character set, allowing the text to have accented letters. Returns an error message for unknown characters.

Examples edit

  • {{ Char |0123456789ABCDEF|15}} → E
  • {{ Char |ÁBĆĎÉf-ABCDEF|4}} → Ď
  • {{ Str len |{{ Char |a b|1}}}} → 1
  • {{ Str len |{{ Char |a b|2}}}} → 0
  • {{ Str len |{{ Char |a b|3}}}} → 1

The last example shows extraction of letter "b" with string length as 1. The prior example extracts a blank but considers the length as 0, for there are no characters. If surrounded by 2 other characters, then the length would be totalled as 3 characters:

  • {{ Str len |<{{ Char |a b|2}}>}} → 3
  • {{ Str len |X{{ Char |a b|2}}X}} → 3
  • {{ Str len |X{{ Str index any |a b|2}}X}} → 3
  • X{{ Char |a b|2}}X → X X

A string-length problem occurs when using template {{Str index any}}.

See also edit

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}}

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: