JS: String.prototype.slice
str.slice(start)
-
Return a substring from index start to end.
console.log("abcd".slice(1, 3) === "bc");
str.slice(start, end)
-
Return a substring from index start to end.
console.log("abcd".slice(0, "abcd".length) === "abcd"); console.log("abcd".slice() === "abcd"); console.log("abcd".slice(1) === "bcd"); console.log("abcd".slice(-1) === "d"); console.log("abcd".slice(-2) === "cd"); // same index, empty string console.log("abcd".slice(2, 2) === ""); // when start index is greater than end index, result is empty string console.log("abcd".slice(3, 2) === ""); console.log("abcd".slice(-2, -3) === ""); // all true
🛑 WARNING: "😂".length === 2
〔see JS: String Code Unit〕
JavaScript, substring
JS String.prototype
- JS: String.prototype.constructor
- JS: String.prototype.at
- JS: String.fromCharCode
- JS: String.prototype.concat
- JS: String.prototype.repeat
- JS: String.prototype.trim
- JS: String.prototype.trimStart
- JS: String.prototype.trimEnd
- JS: String.prototype.padStart
- JS: String.prototype.padEnd
- JS: String.prototype.slice
- JS: String.prototype.substring
- JS: String.prototype.substr
- JS: String.prototype.indexOf
- JS: String.prototype.lastIndexOf
- JS: String.prototype.includes
- JS: String.prototype.startsWith
- JS: String.prototype.endsWith
- JS: String.prototype.search
- JS: String.prototype.match
- JS: String.prototype.matchAll
- JS: String.prototype.replace
- JS: String.prototype.split
- JS: String.prototype.toLowerCase
- JS: String.prototype.charAt
- JS: String.prototype.charCodeAt
- JS: String.prototype.codePointAt