Javascript: substr() v.s. substring()
July 12, 2007 § 16 Comments
substr() method extracts a specified number of characters in a string, from a start index.
Syntax: string.substr(start, length);
substring() method extracts the chars in a string between two specified indexes.
Syntax: string.substring(start, stop);
Quick Notes aout Substr:
- If start is negative, Internet Explorer returns the whole string. That’s wrong! IE should use the last character in the string.
Quick Notes about Substring:
- If start equals stop
,
it returns an empty string. - If stop is omitted, it extracts characters to the end of the string.
- If either argument is less than 0 or is NaN, it is treated as if it were 0.
- If either argument is greater than string’s length, either argument will use string’s length.
- If start > stop, then substring will swap those 2 arguments.
UPDATE (Added Slice upon request)
slice() works like substring() with a few different behaviors.
Syntax: string.slice(start, stop);
Quick Notes about Slice:
- If stop is omitted, slice extracted chars to the end of the string, exactly like substring().
- If start > stop, slice() will NOT swap the 2 arguments.
- If start is negative, slice() will set char from the end of string, exactly like substr() in Firefox. This behavior is observed in both Firefox and IE.
- If stop is negative, slice() will set stop to: (string.length – 1) – stop (original value).
References:
- Mozilla Developer Documentation – substr()
- Mozilla Developer Documentation – substring()
- Mozilla Developer Documentation – slice()
- MSDN Documentation – substr()
- MSDN Documentation – substring()
- MSDN Documentation – slice()
- Opera ECMAScript &/ Javascript Support (including Opera for Wii)
thanks,
your coding and suggessions impressed me and it was helpful.
i got first time the subtle difference between Javascript: substr() v.s. substring()
thanks for the GREAT post! Very useful…
very useful post!
You mean, IE doesn’t do what its supposed to do with substr? I’m shocked!
Well do not be shocked. IE is one of among other terrible products of MS.
Sure pick the easy one. Try blogging about the difference between substring and slice. (It’s a little more subtle)
I hate IE. I hate IE. I hate IE. I hate IE. I hate IE. I hate IE. I hate IE.
One more time..I hate IE!!!! It’s like the George Bush of browsers. It just won’t go away and messes up everything. Makes up it’s own rules. And thinks it’s owns the world. Gahhh.
[…] work taking the file type away. Thus, after asking Google, I found the solution for this problem in this useful post from the “Rudimentary Art of Programming & Development” blog: in JavaScript there […]
thanks for the tutorial
As a Newbie, I am always searching online for articles that can help me. Thank you
quick question. I was trying to understand the difference on a simple “hello world” example.
for substring(-2,7) the result is “Hello W”
But slice() returns nothing. Could you clarify the same?
With slice(), the first parameter is starting index.
Thus, this is what you should do: “hello world”.slice(0, 7)
slice(-2, 7)
==> slice(-2 + str.length, 7)
==> slice(9, 7)
==> “”
i’ve been trying get the 6 chars at the end of a string, but this doesnt work in IE:
str.substr(-1,6); or str.substring(-1,6);
heres a way to do this,
var len = str.length-6;
var endof = str.substring(len, 6);
alert(endof); // gets the last 6 chars in IE.
Thanks! i spended 3 hours of a solution, an i see this
“If start is negative, Internet Explorer returns the whole string. That’s wrong! IE should use the last character in the string.”
now i go to kill developer of internet explorer 🙂 (is a joke!)