{rtrim:string:chars}

Description

Strips characters from the RIGHT (end) of a string and returns the result. You name the characters to strip in the second argument, which is a SET of single characters, not a substring to match: chars set "as" strips any trailing a or s, in any order, until a character that is not in the set is reached. Only the right end is affected - the left side and the interior are left untouched. IMPORTANT: unlike PHP rtrim called with a single argument, {rtrim:text} with NO second argument strips NOTHING. The command always passes a second argument to PHP rtrim and it defaults to an empty string, and an empty character set removes no characters. To strip trailing whitespace you must list those characters yourself, for example {rtrim:text: } for spaces. Unlike most commands, rtrim does not pre-trim its own arguments, so trailing whitespace you pass in is preserved until you strip it. For the left end use ltrim; for both ends use trim.

Parameters

string optional default (empty string)

The input string whose right (trailing) end will be trimmed.

chars optional default (empty string - and an empty set strips nothing)

A SET of single characters to strip from the right end (not a substring). Any trailing character that appears in this set is removed, repeatedly, until a character not in the set is reached. The default is an empty string; because an empty set matches no characters, omitting this argument strips NOTHING. To remove whitespace, list it explicitly, for example a single space, or space and tab.

Examples

test[{rtrim:ananas:as}]
Expected[anan]
Actual[anan]
A common mistake: the second argument is not a word to match at the end, it is a set of single characters. With set a-and-s, the trailing s is removed, then the a before it, and stripping stops at n. So ananas becomes anan, not anana.
test[{rtrim:12300:0}]
Expected[123]
Actual[123]
Stripping trailing zeros. Note this is text, not numeric rounding - 12300 becomes 123 because the two trailing zero characters are removed.
test[{rtrim:xxhelloxx:x}]
Expected[xxhello]
Actual[xxhello]
rtrim works on the right end only - the leading characters are left alone. Use ltrim to strip the left end, or trim to strip both ends.
test[{rtrim:one, two, three, :, }]
Expected[one, two, three]
Actual[one, two, three]
The second argument is a SET of characters - here a comma and a space. Any trailing comma or space is removed, in any order, so a list built with a trailing separator is cleaned up. The commas between words survive because they are not at the end.
test{rtrim:/path/to/dir///:/}
Expected/path/to/dir
Actual/path/to/dir
Pass the slash as the second argument to drop any number of trailing slashes - handy for normalising a path or base URL before appending to it. Only the right end is touched; the leading slash stays.
test[{rtrim:aaabbbccc:c}]
Expected[aaabbb]
Actual[aaabbb]
The second argument names the character(s) to remove. Here every trailing c is stripped; stripping stops at the first b, which is not in the set.
test{rtrim:Hello world : }
ExpectedHello world
ActualHello world
To strip trailing whitespace you must name the characters in the second argument - here a single space. Note that {rtrim:text} with NO second argument strips NOTHING (it passes an empty character set to PHP rtrim, which removes nothing), so the spaces would survive. Add a space to the set to remove them.
test[{rtrim:Hello world }]
Expected[Hello world ]
Actual[Hello world ]
A surprising but important detail: unlike PHP rtrim called with one argument, {rtrim:text} in AA strips NOTHING. The command always passes a second argument to PHP rtrim, defaulting to an empty string, and an empty character set removes no characters. The three trailing spaces survive. To actually strip whitespace, list the characters explicitly, for example {rtrim:text: } for spaces.