toStartCase — GTM Variable Template for String
toStartCase CORE String
Converts a string to Start Case format by capitalizing the first letter of each word. Returns undefined if the input is not a valid string.
When to Use This
String Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Type Conversion
Safely convert between data types — strings, numbers, booleans, arrays, objects.
Formatting
Normalize casing, spacing, encoding, and presentation of data values.
Examples
Underscores to Start Case
INPUT
String To Convert: hello_world_test
OUTPUT
Hello World Test
Hyphens to Start Case
INPUT
String To Convert: hello-world-test
OUTPUT
Hello World Test
Non-string input returns undefined
INPUT
String To Convert: 12345
OUTPUT
undefined
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
toStartCase
String To Convert
💾 The string to convert to Start Case format.
Supported formats:
✓ String
Supported formats:
✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
String To Convert string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
toStartCase()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Converts a string to Start Case format.
*
* @param {string} data.src - The string to convert to Start Case.
* @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 conversion.
*
* @returns {string|undefined} The string in Start Case format, or undefined if the input is not a valid string.
*
* @framework ggLowCodeGTMKit
*/
const toStartCase = function(string) {
if (typeof string === 'string') {
const pattern = "[_\\s-]+";
const replacement = ' ';
const replaceAllWithRegex = function(input, pattern, replacement) {
if (typeof input !== 'string' || typeof pattern !== 'string' || typeof replacement !== 'string') {
return input;
}
let result = input;
let lastIndex = 0;
while (lastIndex < result.length) {
const remainingStr = result.substring(lastIndex);
const matchObj = remainingStr.match(pattern);
if (matchObj === null) {
break;
}
const actualMatchIndex = lastIndex + (matchObj.index || 0);
result = result.substring(0, actualMatchIndex) +
replacement +
result.substring(actualMatchIndex + matchObj[0].length);
lastIndex = actualMatchIndex + replacement.length;
}
return result;
};
let result = replaceAllWithRegex(string, pattern, replacement);
result = result.trim();
result = result.toLowerCase().split(' ')
.map(word => {
if (word.length === 0) return word;
return word[0].toUpperCase() + word.substring(1);
})
.join(' ');
return result;
}
return undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// toStartCase - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(toStartCase(value));
// ===============================================================================
// toStartCase() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(toStartCase(value));
};
*/🧪 View Test Scenarios (5 tests)
✅ Lowercase string with spaces - should convert to Start Case
✅ '[example] Underscores to Start Case'
✅ '[example] Hyphens to Start Case'
✅ Mixed case string with multiple separators - should convert to Start Case
✅ '[example] Non-string input returns undefined'