Skip to content

trimStart — GTM Variable Template for String

VARIABLES › STRING
trimStart CORE String

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


When to Use This

String Manipulation

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


Examples

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
""

GTM Configuration

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()


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 ? data.chr : chars;
  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'