If substr will be dropped in the future, then you can use this replacement, instead of rewriting all your JavaScript codes. It is written according to ECMA specification: https://tc39.es/ecma262/multipage/additional-ecmascript-features-for-web-browsers.html#sec-string.prototype.substr
B.2.2.1 String.prototype.substr ( start, length ) This method returns
a substring of the result of converting the this value to a String,
starting from index start and running for length code units (or
through the end of the String if length is undefined). If start is
negative, it is treated as sourceLength + start where sourceLength is
the length of the String. The result is a String value, not a String
object.
It performs the following steps when called:
- Let O be ? RequireObjectCoercible(this value).
- Let S be ? ToString(O).
- Let size be the length of S.
- Let intStart be ? ToIntegerOrInfinity(start).
- If intStart = -∞, set intStart to 0.
- Else if intStart < 0, set intStart to max(size + intStart, 0).
- Else, set intStart to min(intStart, size).
- If length is undefined, let intLength be size; otherwise let intLength be ? ToIntegerOrInfinity(length).
- Set intLength to the result of clamping intLength between 0 and size.
- Let intEnd be min(intStart + intLength, size).
- Return the substring of S from intStart to intEnd.
String.prototype.substr = function (start, length)
{
const S = this.toString(); // step 1 and 2
const size = S.length; // step 3
let intStart = Number.isNaN(Number(start)) ? 0 : Number.parseInt(start); // step 4
if (intStart === -Infinity) intStart = 0; // step 5
else if (intStart < 0) intStart = Math.max(size + intStart, 0); // step 6
else intStart = Math.min(intStart, size); // step 7
let intLength = length === undefined ? size : (Number.isNaN(Number(length)) ? 0 : Number.parseInt(length)); // step 8
intLength = Math.max(Math.min(intLength, size), 0); // step 9
let intEnd = Math.min(intStart + intLength, size); // step 10
return S.substring(intStart, intEnd); // step 11
};
.. and tested using this script (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr):
const aString = "Mozilla";
console.log(aString.substr(0, 1)); // 'M'
console.log(aString.substr(1, 0)); // ''
console.log(aString.substr(-1, 1)); // 'a'
console.log(aString.substr(1, -1)); // ''
console.log(aString.substr(-3)); // 'lla'
console.log(aString.substr(1)); // 'ozilla'
console.log(aString.substr(-20, 2)); // 'Mo'
console.log(aString.substr(20, 2)); // ''
substring
is intended to replace it - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… - substring takes a start and end position which is more conventional than start + number of characters. – Flabbergast.substring
method sounds very similar, but uses end index as a second parameter rather than length. Easy to mix up, probably, better to just use one method, if possible – Uveitissubstring
has been in the standardized language since there was one, butsubstr
never has. :-) – Misciblesubstr
to the standards, since many prefer using substr over substring? (arguing that using a length can be easier than using an index) – Coronographsubstr
byslice
. Works identical with strings. – Pfeifer> "12345678".substr( -2 , 2) '78' vs "12345678".slice( -2 , 2) ''
– Iy