String Functions
at
str.at(index?: integer = 0): stringReturns the character at the given index.
- When the index is out of bounds, it will return
niltype, which will be converted to an empty string when rendered in the template - When the index is negative, it will count from the end of the string. For example,
-1will return the last character of the string
Arguments:
index(integer) - The index of the character to return. Default is0, which returns the first character of the string
Input Example:
{{ "Textwire".at(1) }}Output:
ecapitalize
str.capitalize(): stringCapitalizes the first letter of a string.
Input Example:
{{ "hello, world!".capitalize() }}Output:
Hello, world!contains
str.contains(substr: string): booleanReturns true if the string contains the given substring, otherwise false. Booleans render as 1 and 0 when printed directly. The function is case-sensitive, so the substring must match the case of the string.
Arguments:
substr(string) - The substring to search for
Input Example:
{{ "Hello, World!".contains("World") }}Output:
1Empty Substring
If the substr argument is an empty string, the function will always return true (1 when rendered). It's done this way because an empty string is always a substring of any string
Ignore Case
If you want to search for a substring without considering the case, you can use the lower function to convert both the string and the substring to lowercase:
{{ "Hello, World!".lower().contains("world") }} {{-- true --}}decimal
str.decimal(separator?: string = ".", decimals?: integer = 2): stringEnsures a numeric value has at least the requested number of decimal places. It pads shorter values with zeroes and leaves values that already have enough decimal places unchanged. It does not round or remove extra decimal places.
- Integers are padded with zeroes:
123.decimal()returns string"123.00" - Numeric strings that already have enough decimal places are returned unchanged:
"123.456".decimal()returns string"123.456" - Numeric strings with fewer decimal places are padded:
"123.4".decimal()returns string"123.40" - Strings that are not numbers are returned unchanged
decimals defaults to 2. Negative values are treated as 0.
Arguments:
separator(str) (optional) - The separator to use for the decimal. Default is"."decimals(int) (optional) - How many decimal places to ensure. Default is2. Negative values are treated as0
Input Example:
{{ "123".decimal() }}
{{ "123".decimal(".", 0) }}
{{ "123".decimal(".", -1) }}Output:
123.00
123
123first
str.first(): stringReturns the first character of a string. When the string is empty, it will return nil type, which will be converted to an empty string when rendered in the template.
Input Example:
{{ "Textwire".first() }}Output:
Tformat
str.format(arg: any...): stringFunction format is used to format a string using placeholders. The placeholders are represented by %s in the string, and they will be replaced by the corresponding arguments passed to the function. The number of placeholders in the string should match the number of arguments passed to the function, otherwise, placeholders are just going to be rendered as %s.
Arguments:
arg(any) - Any amount of arguments
Input Example:
{{ "I have %s apples, they are %s".format(3, "tasty") }}Output:
I have 3 apples, they are tastyImportant Notes
- Too many arguments. If you pass more arguments than the string needs, the rest will be ignored.
- Not enough arguments. If you pass not enough arguments, the remaining placeholders will be rendered as
%s. - Required argument. Requires at least 1 argument.
- Conversion to string. All the arguments passed to the function will be converted to string.
last
str.last(): stringReturns the last character of a string. When the string is empty, it will return nil type, which will be converted to an empty string when rendered in the template.
Input Example:
{{ "Textwire".last() }}Output:
elen
str.len(): integerReturns the length of the string.
Input Example:
{{ "Hello, World!".len() }}Output:
13lower
str.lower(): stringConverts a string to lowercase.
Input Example:
{{ "Hello, World!".lower() }}Output:
hello, world!repeat
str.repeat(times: integer): stringReturns a new string consisting of count copies of the string on which it was called.
Arguments:
times(integer) - The number of times to repeat the string
Input Example:
{{ "Hello".repeat(3) }}Output:
HelloHelloHelloreverse
str.reverse(): stringReturns a string with the characters reversed. Doesn't mutate the original string.
Input Example:
{{ "stressed".reverse() }}Output:
dessertssplit
str.split(separator?: string = " "): arrayFunction split is used to split a string into an array of substrings. It takes an optional argument separator which is used to split the string. If no separator is provided, it defaults to a space.
Arguments:
separator(string) (optional) - What separator to use to split the string. Default is" "(space)
Input Example:
<div>{{ "one two".split(" ") }}</div>Output:
<div>one, two</div>trim
str.trim(chars?: string = "\t \n\r"): stringTrims a string from spaces and special characters like tabs, spaces and new lines by default. You can pass an argument to trim a specific set of characters from a string.
Arguments:
chars(string) (optional) - A string of characters to trim from a string. Default is"\t \n\r"
Input Example:
<span>{{ " Textwire ".trim() }}</span>Output:
<span>Textwire</span>trimLeft
str.trimLeft(chars?: string = "\t \n\r"): stringTrims left side of a string from spaces and special characters like tabs, spaces and new lines by default. You can pass an argument to trim a specific set of characters from a string.
Arguments:
chars(string) (optional) - A string of characters to trim from a string. Default is"\t \n\r"
Input Example:
<span>{{ " Textwire".trimLeft() }}</span>Output:
<span>Textwire</span>trimRight
str.trimRight(chars?: string = "\t \n\r"): stringTrims right side of a string from spaces and special characters like tabs, spaces and new lines by default. You can pass an argument to trim a specific set of characters from a string.
Arguments:
chars(string) (optional) - A string of characters to trim from a string. Default is"\t \n\r"
Input Example:
<span>{{ "Textwire ".trimRight() }}</span>Output:
<span>Textwire</span>truncate
str.truncate(length: integer, ellipsis: string = "..."): stringReturns a string truncated to the given length with an optional ellipsis at the end.
Arguments:
length(integer) - The length to truncate the string toellipsis(string) (optional) - The ellipsis to append to the truncated string. Default is"..."
Input Example:
{{ "Hello, World!".truncate(5) }}Output:
Hello...upper
str.upper(): stringConverts a string to uppercase.
Input Example:
{{ "Hello, World!".upper() }}Output:
HELLO, WORLD!