Skip to content

trimEnd β€” GTM Variable Template for String

VARIABLES β€Ί STRING
trimEnd CORE String
Direct (.tpl) Apply (.tpl)

Trims whitespace or specified characters from the end of a string, removing trailing characters as specified.


String Manipulation

Transform, clean, and normalize text data for consistent downstream processing.


Trim custom characters
INPUT
String To Trim: hello world...
Characters To Remove: .
OUTPUT
hello world
String with trailing dots
INPUT
String To Trim: test123!!!
Characters To Remove: !123
OUTPUT
test
Non-string returns empty string
INPUT
String To Trim: 12345
Characters To Remove: undefined
OUTPUT
""

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

trimEnd
String To Trim
πŸ’Ύ The string to trim from the end.

Supported formats:
  βœ“ String
Characters To Remove
πŸ”€ The characters to remove from the end (used when custom characters is enabled, default is whitespace). Each character is considered independently.

↳If characters like yi are specified, then both y and i will be removed from the end of the string, regardless of the order or frequency of occurrence.

Supported formats:
  βœ“ String
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before internal logic (e.g., normalize case, convert to string). Internal transformations such as string validation will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., str => str.toUpperCase(), val => val + ' trimmed' for string modifications). Useful for chaining transformations on the output.
String To Trim string
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Characters To Remove string
trimEnd()


πŸ“œ View Implementation Code
/**
* Trims whitespace or specified characters from the end of a string.
*
* @param {string} data.src - The string to trim.
* @param {string} data.chr - The characters to remove (default is whitespace if not provided).
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before trimming.
*
* @returns {string} The string with trailing whitespace or specified characters removed.
*
* @framework ggLowCodeGTMKit
*/
const trimEnd = function(string, chars) {
const charsToTrim = chars !== undefined ? chars : " ";
if (typeof string !== 'string') { return ""; }
let endIndex = string.length;
while (endIndex > 0 && charsToTrim.indexOf(string.charAt(endIndex - 1)) !== -1) {
endIndex--;
}
return string.slice(0, endIndex);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// trimEnd - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(trimEnd(value, data.chr));
// ===============================================================================
// trimEnd(...) – Apply Mode
// ===============================================================================
/*
return function(value, chars) {
chars = data.rp1 ? chars : data.chr;
return out(trimEnd(value, chars));
};
*/
πŸ§ͺ View Test Scenarios (7 tests)
βœ… Trim trailing spaces
βœ… Trim no trailing spaces
βœ… '[example] Trim custom characters'
βœ… '[example] String with trailing dots'
βœ… Empty string
βœ… String with mixed trailing characters
βœ… '[example] Non-string returns empty string'