Skip to content

String Functions ​

at ​

ts
str.at(index?: integer = 0): string

Returns the character at the given index.

  • When the index is out of bounds, it will return nil type, 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, -1 will return the last character of the string

Arguments: ​

  1. index (integer) - The index of the character to return. Default is 0, which returns the first character of the string

Input Example: ​

textwire
{{ "Textwire".at(1) }}

Output: ​

html
e

capitalize ​

ts
str.capitalize(): string

Capitalizes the first letter of a string.

Input Example: ​

textwire
{{ "hello, world!".capitalize() }}

Output: ​

html
Hello, world!

contains ​

ts
str.contains(substr: string): boolean

Returns 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: ​

  1. substr (string) - The substring to search for

Input Example: ​

textwire
{{ "Hello, World!".contains("World") }}

Output: ​

html
1

Empty 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:

textwire
{{ "Hello, World!".lower().contains("world") }} {{-- true --}}

decimal ​

ts
str.decimal(separator?: string = ".", decimals?: integer = 2): string

Ensures 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: ​

  1. separator (str) (optional) - The separator to use for the decimal. Default is "."
  2. decimals (int) (optional) - How many decimal places to ensure. Default is 2. Negative values are treated as 0

Input Example: ​

textwire
{{ "123".decimal() }}
{{ "123".decimal(".", 0) }}
{{ "123".decimal(".", -1) }}

Output: ​

html
123.00
123
123

first ​

ts
str.first(): string

Returns 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
{{ "Textwire".first() }}

Output: ​

html
T

format ​

ts
str.format(arg: any...): string

Function 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: ​

  1. arg (any) - Any amount of arguments

Input Example: ​

textwire
{{ "I have %s apples, they are %s".format(3, "tasty") }}

Output: ​

html
I have 3 apples, they are tasty

Important 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 ​

ts
str.last(): string

Returns 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
{{ "Textwire".last() }}

Output: ​

html
e

len ​

ts
str.len(): integer

Returns the length of the string.

Input Example: ​

textwire
{{ "Hello, World!".len() }}

Output: ​

html
13

lower ​

ts
str.lower(): string

Converts a string to lowercase.

Input Example: ​

textwire
{{ "Hello, World!".lower() }}

Output: ​

html
hello, world!

repeat ​

ts
str.repeat(times: integer): string

Returns a new string consisting of count copies of the string on which it was called.

Arguments: ​

  1. times (integer) - The number of times to repeat the string

Input Example: ​

textwire
{{ "Hello".repeat(3) }}

Output: ​

html
HelloHelloHello

reverse ​

ts
str.reverse(): string

Returns a string with the characters reversed. Doesn't mutate the original string.

Input Example: ​

textwire
{{ "stressed".reverse() }}

Output: ​

html
desserts

split ​

ts
str.split(separator?: string = " "): array

Function 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: ​

  1. separator (string) (optional) - What separator to use to split the string. Default is " " (space)

Input Example: ​

textwire
<div>{{ "one two".split(" ") }}</div>

Output: ​

html
<div>one, two</div>

trim ​

ts
str.trim(chars?: string = "\t \n\r"): string

Trims 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: ​

  1. chars (string) (optional) - A string of characters to trim from a string. Default is "\t \n\r"

Input Example: ​

textwire
<span>{{ " Textwire ".trim() }}</span>

Output: ​

html
<span>Textwire</span>

trimLeft ​

ts
str.trimLeft(chars?: string = "\t \n\r"): string

Trims 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: ​

  1. chars (string) (optional) - A string of characters to trim from a string. Default is "\t \n\r"

Input Example: ​

textwire
<span>{{ " Textwire".trimLeft() }}</span>

Output: ​

html
<span>Textwire</span>

trimRight ​

ts
str.trimRight(chars?: string = "\t \n\r"): string

Trims 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: ​

  1. chars (string) (optional) - A string of characters to trim from a string. Default is "\t \n\r"

Input Example: ​

textwire
<span>{{ "Textwire ".trimRight() }}</span>

Output: ​

html
<span>Textwire</span>

truncate ​

ts
str.truncate(length: integer, ellipsis: string = "..."): string

Returns a string truncated to the given length with an optional ellipsis at the end.

Arguments: ​

  1. length (integer) - The length to truncate the string to
  2. ellipsis (string) (optional) - The ellipsis to append to the truncated string. Default is "..."

Input Example: ​

textwire
{{ "Hello, World!".truncate(5) }}

Output: ​

html
Hello...

upper ​

ts
str.upper(): string

Converts a string to uppercase.

Input Example: ​

textwire
{{ "Hello, World!".upper() }}

Output: ​

html
HELLO, WORLD!