trimStart β GTM Variable Template for String
trimStart CORE String
Trims whitespace or specified characters from the start of a string, removing leading 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 leading characters
INPUT
String To Trim: ###hello###
Characters To Remove: #
Characters To Remove: #
OUTPUT
hello###
No leading whitespace unchanged
INPUT
String To Trim: hello world
Characters To Remove:
Characters To Remove:
OUTPUT
hello world
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.
trimStart
String To Trim
πΎ The string to trim from the start.
Supported formats:
β String
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
β³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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
trimStart()
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 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'