Possible Duplicate:
What is the difference between substr and substring?
I just want to clarify this differences of:-
alert("abc".substr(0,2));
alert("abc".substring(0,2));
the above case both will return
ab
alert("abc".substr(1,2));
alert("abc".substring(1,2));
and the above case will return first bc and the second one b
so basically for the substr the second argument is the length it has to go?
and for substring it will stop but not include??
Please correct me if I’m wrong because we will have a quiz today and I don’t want to get this wrong again.
Yes,
substr()‘s second parameter is the length of the required substring whilesubstring()‘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 onsubstr()) and had some bugs in older versions of IE. Also,slice()is preferable tosubstring()because it allows negative character indices, counted backwards from the end of the string: