Differencebetween Substr and Substring

What is the difference between substr and substring?

The difference is in the second argument. The second argument to substring is the index to stop at (but not include), but the second argument to substr is the maximum length to return.

Links?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

What is the different between substr and substring?

Yes, substr()'s second parameter is the length of the required substring while substring()'s is a character index, a fact which is pretty trivial to find via a web search.

What is less well-known is that substr() is non-standard (up to and including ECMAScript 5; ES3 and ES5 have non-normative sections on substr()) and had some bugs in older versions of IE. Also, slice() is preferable to substring() because it allows negative character indices, counted backwards from the end of the string:

alert( "Dandelion".slice(-4) ); // Alerts "lion"

What is the difference between substring() and substr() in MySQL?

No difference. Read the manual!

Why historically was `substr` added when `substring` already existed?

I don't think we can definitively answer this without someone involved with ECMA ten years ago weighing in, however we can infer some things.

substring asks for the index (from 0) of the start, and optionally an index (again from 0) of the end.

substr asks for the index (from 0) of the start, and a length. That makes it simpler to use for some cases, e.g. if you want to fetch 2 characters from the first "s" you can just specify 2 as the length instead of having to first calculate the index of the first "s" and then adding 2 to that, and then passing both positions to substring.

As was pointed out in a comment, substr allows a negative starting position.

Arguably, substr is a better implementation. And so that is quite possibly why it was added.

As to the question of why are both there, that is probably for backward compatibility. Because Javascript runs in a wide variety of browsers (and these days elsewhere), in runtimes maintained by multiple organizations, there is no easy way to deprecate or eliminate anything. It's easier to just leave it there and add more to the language instead. Old code still works, and new code has better options available to use.

Substring vs Slice vs Other?

The difference between them is:

The substring() method swaps its two arguments if indexStart is greater than indexEnd, meaning that a string is still returned. The slice() method returns an empty string if this is the case.

If either or both of the arguments are negative or NaN, the substring() method treats them as if they were 0.

slice() also treats NaN arguments as 0, but when it is given negative values it counts backwards from the end of the string to find the indexes.

Since you already have a reference to the string as this.value, and since you can calculate the lower and upper indicies directly (without passing negative indicies or NaN, or anything silly like that), it makes absolutely no difference whether you use substring or slice if you want to insert a particular character. Use whichever you want, it won't have any effect for an operation like this.

this.value = this.value.slice(0, selectionEnd) + 'z' + this.value.slice(selectionEnd);

or

this.value = this.value.substring(0, selectionEnd) + 'z' + this.value.substring(selectionEnd);

both work fine.

input.onkeydown = function(e){
const { selectionEnd } = this;
this.value = this.value.substring(0, selectionEnd) + 'z' + this.value.substring(selectionEnd);
this.selectionEnd = selectionEnd + 1;
return false;
}
<input id="input" value="abc">

What is the difference between slice() and substr() in JavaScript?

They have different signatures, .slice() is:

string.slice(beginIndex, endIndex)

Whereas .substr() is:

string.substr(beginIndex, length);

So for example, if we have "1234" and wanted "23", it would be:

"1234".slice(1,3)
//or...
"1234".substr(1,2)

They also have different behavior for the more-rarely used negative indexes, look at the MDC documentation for .slice() and .substr() for full descriptions.



Related Topics



Leave a reply



Submit