Skip to content

trimStart β€” GTM Variable Template for String

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

Trims whitespace or specified characters from the start of a string, removing leading characters as specified.


String Manipulation

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


Trim custom leading characters
INPUT
String To Trim: ###hello###
Characters To Remove: #
OUTPUT
hello###
No leading whitespace unchanged
INPUT
String To Trim: hello world
Characters To Remove:
OUTPUT
hello world
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.

trimStart
String To Trim
πŸ’Ύ The string to trim from the start.

Supported formats:
  βœ“ String
Characters To Remove
πŸ”€ The characters to remove from the start (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 start 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
trimStart()


πŸ“œ View Implementation Code
/**
* Trims whitespace or specified characters from the start of a string.
*
* @param {string} data.src - The string to trim.
* @param {string} data.chr - The characters to remove (used when add is true, default is whitespace).
* @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 leading whitespace or specified characters removed.
*
* @framework ggLowCodeGTMKit
*/
const trimStart = function(string, chars) {
const charsToTrim = chars !== undefined ? chars : " ";
if (typeof string !== 'string') { return ""; }
let startIndex = 0;
while (startIndex < string.length && charsToTrim.indexOf(string.charAt(startIndex)) !== -1) {
startIndex++;
}
return string.slice(startIndex);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// trimStart - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(trimStart(value, data.chr));
// ===============================================================================
// trimStart() – Apply Mode
// ===============================================================================
/*
return function(value, chars) {
chars = data.rp1 ? chars : data.chr;
return out(trimStart(value, chars));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… Trim leading spaces
βœ… '[example] Trim custom leading characters'
βœ… '[example] No leading whitespace unchanged'
βœ… String with leading tabs and newlines - should not remove them without custom chars usage
βœ… '[example] Non-string returns empty string'