String Methods
String prototype methods in SSJS — which work natively, which are partial, and which are missing, with ES3/ES5-safe alternatives and polyfill links.
Each member below is tagged with the ECMAScript edition that standardized it: (ES3), (ES5), or (ES6). Methods that need a polyfill link to Polyfills.
Status legend
| Icon | Meaning |
|---|---|
| ✅ Works | Available and behaves as expected |
| ⚠️ Partial | Available but with a documented caveat or bug |
| ❌ Missing | Not available — use the workaround / polyfill |
Members
| Member | ES | Status | Notes |
|---|---|---|---|
length |
ES3 | ✅ Works | |
charAt(index) |
ES3 | ✅ Works | str[i] also works |
charCodeAt(index) |
ES3 | ✅ Works | |
indexOf(search, fromIndex) |
ES3 | ✅ Works | |
lastIndexOf(search, fromIndex) |
ES3 | ✅ Works | |
toUpperCase() |
ES3 | ✅ Works | |
toLowerCase() |
ES3 | ✅ Works | |
toLocaleLowerCase() |
ES3 | ✅ Works | |
substring(start, end) |
ES3 | ✅ Works | |
slice(start, end) |
ES3 | ✅ Works | |
concat(...strings) |
ES3 | ✅ Works | |
replace(pattern, replacement) |
ES3 | ✅ Works | |
localeCompare(other) |
ES3 | ✅ Works | |
match(regexp) |
ES3 | ⚠️ Partial | Returns [] (not null) on no match; no .index |
search(regexp) |
ES3 | ⚠️ Partial | Returns 0 (not -1) on no match; unreliable — see Polyfills |
split(separator, limit) |
ES3 | ⚠️ Partial | Empty-separator form does not split into chars — see Polyfills |
trim() |
ES5 | ❌ Missing | See Polyfills, or Platform.Function.Trim |
substr(start, length) |
ES3 | ❌ Missing | Throws at runtime — use substring/slice or polyfill |
startsWith(prefix) |
ES6 | ❌ Missing | Use indexOf(prefix) === 0 or polyfill |
endsWith(suffix) |
ES6 | ❌ Missing | Use lastIndexOf check or polyfill |
includes(substr) |
ES6 | ❌ Missing | Use indexOf(substr) !== -1 |
trimStart() |
ES6 | ❌ Missing | Use a /^\s+/ replace |
trimEnd() |
ES6 | ❌ Missing | Use a /\s+$/ replace |
padStart(targetLen, pad) |
ES6 | ❌ Missing | Prepend pad characters in a loop |
padEnd(targetLen, pad) |
ES6 | ❌ Missing | Append pad characters in a loop |
repeat(count) |
ES6 | ❌ Missing | Concatenate in a loop |
codePointAt(index) |
ES6 | ❌ Missing | Use charCodeAt for BMP characters |
length
(ES3) — ✅ Works. The number of UTF-16 code units in the string.
"Hello".length; // 5
charAt
(ES3) — ✅ Works. Returns the character at the given index. str[i] also works in SSJS.
"Hello".charAt(0); // "H"
"Hello"[0]; // "H"
charCodeAt
(ES3) — ✅ Works. Returns the UTF-16 code unit at the given index.
"Hello".charCodeAt(0); // 72
indexOf
(ES3) — ✅ Works. Returns the index of the first occurrence, or -1.
"Hello World".indexOf("World"); // 6
"Hello World".indexOf("o", 5); // 7
lastIndexOf
(ES3) — ✅ Works. Returns the index of the last occurrence, or -1.
"Hello World Hello".lastIndexOf("Hello"); // 12
toUpperCase
(ES3) — ✅ Works.
"Hello".toUpperCase(); // "HELLO"
toLowerCase
(ES3) — ✅ Works.
"Hello".toLowerCase(); // "hello"
toLocaleLowerCase
(ES3) — ✅ Works. Locale-aware lowercase.
"Hello".toLocaleLowerCase(); // "hello"
substring
(ES3) — ✅ Works. Returns the part of the string between two indices.
"Hello World".substring(6, 11); // "World"
slice
(ES3) — ✅ Works. Like substring, but supports negative indices.
"Hello World".slice(0, 5); // "Hello"
"Hello World".slice(-5); // "World"
concat
(ES3) — ✅ Works. Concatenates strings. + is usually preferred.
"Hello".concat(" ", "World"); // "Hello World"
replace
(ES3) — ✅ Works. Replaces matches of a string or RegExp. Use the /g flag to replace all.
"aabbcc".replace("b", "X"); // "aaXbcc"
"aabbcc".replace(/b/g, "X"); // "aaXXcc"
"hello world".replace(/\b\w/g, function (c) { return c.toUpperCase(); }); // "Hello World"
localeCompare
(ES3) — ✅ Works. Compares two strings in the current locale.
"apple".localeCompare("banana"); // negative
"apple".localeCompare("apple"); // 0
match
(ES3) — ⚠️ Partial. In SFMC, String.match returns an empty array [] (not null) when there is no match, and matched results do not carry an .index property.
var str = "Call 555-1234 or 555-5678";
str.match(/\d{3}-\d{4}/g); // ["555-1234", "555-5678"]
str.match(/zzz/); // [] (empty array in SFMC, not null)
search
(ES3) — ⚠️ Partial. Unreliable in SFMC: returns 0 instead of -1 for a no-match, and some real matches return the wrong index. Use match or RegExp.test to detect a match, or apply the polyfill.
"abc123".search(/\d/); // unreliable — prefer match() or RegExp.test()
split
(ES3) — ⚠️ Partial. The empty-separator form str.split("") does not split into characters in SFMC — it returns the whole string as a single element. Loop with charAt, or apply the polyfill.
"a,b,c".split(","); // ["a", "b", "c"]
"a b c".split(/\s+/); // ["a", "b", "c"]
// "hello".split("") — ❌ does NOT split into chars in SFMC. Loop instead:
var chars = [];
var s = "hello";
for (var i = 0; i < s.length; i++) { chars.push(s.charAt(i)); }
trim
(ES5) — ❌ Missing. Apply the polyfill, or use Platform.Function.Trim.
function trim(str) {
return String(str).replace(/^\s+/, "").replace(/\s+$/, "");
}
substr
(ES3) — ❌ Missing. String.prototype.substr throws at runtime in SFMC. Use substring or slice, or apply the polyfill.
"Hello World".substring(6, 11); // "World" (instead of substr(6, 5))
startsWith
(ES6) — ❌ Missing. Use indexOf(prefix) === 0 or the polyfill.
function startsWith(str, prefix) {
return str.indexOf(prefix) === 0;
}
endsWith
(ES6) — ❌ Missing. Use a lastIndexOf check or the polyfill.
function endsWith(str, suffix) {
return str.lastIndexOf(suffix) === str.length - suffix.length;
}
includes
(ES6) — ❌ Missing. Use indexOf(substr) !== -1.
function includes(str, sub) {
return str.indexOf(sub) !== -1;
}
trimStart
(ES6) — ❌ Missing. Use a /^\s+/ replace.
function trimStart(str) {
return String(str).replace(/^\s+/, "");
}
trimEnd
(ES6) — ❌ Missing. Use a /\s+$/ replace.
function trimEnd(str) {
return String(str).replace(/\s+$/, "");
}
padStart
(ES6) — ❌ Missing. Prepend pad characters in a loop.
function padStart(str, targetLen, padChar) {
str = String(str);
padChar = padChar || " ";
while (str.length < targetLen) { str = padChar + str; }
return str;
}
padStart("7", 3, "0"); // "007"
padEnd
(ES6) — ❌ Missing. Append pad characters in a loop.
function padEnd(str, targetLen, padChar) {
str = String(str);
padChar = padChar || " ";
while (str.length < targetLen) { str = str + padChar; }
return str;
}
repeat
(ES6) — ❌ Missing. Concatenate in a loop.
function repeat(str, n) {
var result = "";
for (var i = 0; i < n; i++) { result += str; }
return result;
}
repeat("ab", 3); // "ababab"
codePointAt
(ES6) — ❌ Missing. Use charCodeAt for Basic Multilingual Plane characters.
"A".charCodeAt(0); // 65 (codePointAt(0) for BMP chars)