JS: String.prototype.substring 👎
🟢 tip: better is String.prototype.slice
str.substring(start, end)-
return a substring from start to end.
- If any argument is negative, it is replaced by 0.
- If any argument is greater than length, it's replaced by length.
- If start greater than end, swap them
// end does not include itself console.assert("0123".substring(0, 2) === "01"); // end by default is to the end console.assert("0123".substring(1) === "123");
Edge cases
// if start greater than end, swap them console.log("0123456".substring(3, 1)); // 12 // s------------------------------ // if start is negative, replace it by 0 console.log("0123456".substring(-1)); // 0123456 // if end is negative, replace it by 0. // this is same as substring(0,1) console.log("0123456".substring(1, -1)); // 0 // this is same as substring(0,3) console.log("0123456".substring(3, -1)); // 012 // this is same as substring(0,0) console.log("0123456".substring(-3, -1)); // "" // s------------------------------ // no args. return the full string. console.log("0123456".substring()); // 0123456