trimEnd β GTM Variable Template for String
trimEnd CORE String
Trims whitespace or specified characters from the end of a string, removing trailing characters as specified.
When to Use This
Section titled βWhen to Use ThisβString Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Examples
Section titled βExamplesβTrim custom characters
INPUT
String To Trim: hello world...
Characters To Remove: .
Characters To Remove: .
OUTPUT
hello world
String with trailing dots
INPUT
String To Trim: test123!!!
Characters To Remove: !123
Characters To Remove: !123
OUTPUT
test
Non-string returns empty string
INPUT
String To Trim: 12345
Characters To Remove: undefined
Characters To Remove: undefined
OUTPUT
""
Live Sandbox
Section titled βLive Sandboxβ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
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
β³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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
trimEnd()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ 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'