{trim:string:chars}

Description

Strips characters from both ends of a string and returns the result. With one argument it removes whitespace (spaces, tabs, newlines, and other Unicode whitespace) from the start and end. Give a second argument to strip a specific set of characters instead.

Parameters

string required default empty string

The text to trim. May be a literal string or, more usually, a field getter such as {headline........} that yields the value to clean up.

chars optional default whitespace (spaces, tabs, newlines)

Optional set of characters to strip. Each listed character is removed from both ends (it is a character set, not a substring). When omitted, all leading and trailing whitespace is removed (spaces, tabs, newlines, and other Unicode whitespace).

Examples

test{trim: APC-AA }
ExpectedAPC-AA
ActualAPC-AA
The simplest use: with no second argument, trim removes whitespace from both ends. Spaces inside the string are kept.
test[{trim: }]
Expected[]
Actual[]
A string that is only whitespace becomes empty. This is why trim pairs well with ifset to skip rendering blank values.
test{trim:***Bold***:*}
ExpectedBold
ActualBold
The second argument is a SET of characters to strip from both ends. Here every leading and trailing asterisk is removed.
test{trim:00042000:0}
Expected42
Actual42
The character set is stripped from the start AND the end. Both the leading and the trailing zeros are removed here; the zeros between 42 and the ends would also go if present.
test{trim:[hello]:[]}
Expectedhello
Actualhello
Each character listed in the second argument is stripped on its own. Here both the opening and closing bracket are removed; the argument is not matched as the literal text [ ].
test{trim:--a--b--:-}
Expecteda--b
Actuala--b
Only the ends are trimmed. The dashes between a and b are inside the string, so they stay.
test{ifset:{trim: }:has text:blank}
Expectedblank
Actualblank
A common real-world pattern: trim a value first, then test it. A field holding only spaces is then correctly treated as blank, so you do not render an empty wrapper around it.
testtrim=[{trim:..core..:.}] ltrim=[{ltrim:..core..:.}] rtrim=[{rtrim:..core..:.}]
Expectedtrim=[core] ltrim=[core..] rtrim=[..core]
Actualtrim=[core] ltrim=[core..] rtrim=[..core]
trim strips both ends; ltrim strips only the start; rtrim strips only the end. All three take the same optional character-set argument.