Among these are
substr(), and what can be confusing is the fact that all are somewhat similar in functionality. The natural question thus arises: what exactly is different about these? Are they pretty much interchangeable? At the end of the article, you will ideally know the answer.
A note about substr()
First of all,
substr() is considered deprecated and thus not suitable for use in new websites. That simplifies things, doesn’t it? However, you might be finding its usage in legacy code or something, so I think it’s still somewhat valuable to be aware of what it does.
I certainly would not dedicate too much headspace to it, though.
Input parameters: how to call them
start is an integer where you choose the index of the character you want to start slicing from.
end is an integer where you choose the index of the character you want to end the slicing at.
You’ll notice that the parameters here are exactly the same as in
slice(). This is absolutely right; they’re called the same, and they achieve the same results. To reiterate,
start is an integer where you choose the index of the character you want to start the substring from.
end is an integer where you choose the index of the character you want to end the substring at.
substr() is slightly different;
start is still the same parameter as with the previous two methods in that it’s the index of the string where you want to start the substring from. However, the second argument is not the index of the string where you want the substring to end. Rather, it’s a length that counts the characters until your desired end point.
For example, if you specify
3 for the second parameter, it will count three characters from the specified index in the first parameter.
In a nutshell
substring() are the exact same.
substr() is unique in that the second parameter is not the end index number.
Input parameters: the order you put them in
An interesting difference between the functions is the different functionalities depending on the order that you put the input parameters in.
start is greater than
end, you may be confused as to what happens because it doesn’t really make sense to have a starting index after the ending index. With
slice(), you’re correct here; the function won’t perform, and it will actually return the empty string (
start is greater than
substring() actually has special logic where it automatically switches the values. Essentially, the below calls would yield the exact same results — in other words, do the exact same thing.
Both lines 3 and 4 return
Now that I think about it, this might somehow make a good little bet with a programmer friend, because naturally, it’s hard to imagine these end up the same.
The order here certainly matters in that the second argument isn’t
end; rather, it’s the
length or the amount of characters you want to navigate after the index you specify as the first parameter. Thus, you shouldn’t mix the two, and there’s no fundamental or logical issue if the first parameter is greater than the second one.
Input parameters: using only one argument
Good news: if you omit the second argument — and that means only providing one input parameter — then that means all three functions do the exact same thing.
What they all do is get a substring starting from the index in the provided argument. That means for all three of these in the example above, the return result is
Input parameters: negative arguments
end are allowed to be negative, but what happens when you do that? Let’s say
When the arguments are negative with
slice(), you count from the back (the end) of the string. So in this case, you count three characters from the back (note that it does not mean you inverse the negative and start at the index of
3) for the start of the slice, and you count one character from the back for the end of the slice.
So for the start of
-3, let’s count three characters back. That would mean we go past
t, go past
o, and then go past
r and stop there. We’re on
r now for the start.
For the end, we go past
We now have the start at
r and the end at
Your return string is thus
Negative arguments are different than for
slice(). Actually, negative arguments immediately resolve to
0. Thus, if you have
-1, then it’s the same as having them both as
In other words, it doesn’t actually work, and your return string is
"" in this case.
If only one argument is negative, then only that argument turns into
0. The other one remains its positive value.
substring(), just basically don’t use negative values, unless you want to write some confusing code.
First of all, if the second argument (
length in the above example) is negative, then the function simply does not work. The return value will be an empty string (
""). This makes sense logically because a negative length is nonsensical.
The first argument can be negative, and it basically means you start counting from the back of the string, just like for
Thus, if you have
2 , then we start three characters from the back (at the second
a) and then literally count two characters — but to the right. It’s important to know that the function still does what it’s designed to do, and that’s to get the substring to the right.
Thus, the output will be