Somnath Kharat
Somnath Kharat

Reputation: 3600

Why two different methods slice() & substring()?

var name="nameSomnath";//remove name

I can do with slice()

var result = name.slice( 4 );

Same can be done with substring()

var result = name.substring( 4 );

So what makes them different. I have seen the link Here which elaborates the difference .But we can do the same thing by using any one method ie slice() or substring().So why there was need to have two methods.

Upvotes: 4

Views: 5789

Answers (6)

saif
saif

Reputation: 487

slice() and substring() do the same thing with some common behaviors, but there are some distinctions in the handling negative arguments

Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);

Common behaviors:

  1. If start equals stop: returns an empty string
  2. If the stop is omitted: extracts characters to the ethe nd of the string
  3. If either argument is greater than the string's length, the string's length will be used instead.

Distinctions of substring():

  1. If start > stop, then substring will swap those 2 arguments.
  2. If either argument is negative or is NaN, it is treated as if it were 0.

Distinctions of slice():

  1. If start > stop, slice() will return the empty string. ("")
  2. If the start is negative: sets char from the end of the string, exactly like substr() in Firefox. This behavior is observed in both Firefox and IE.
  3. If the stop is negative: sets stop to: string.length – Math.abs(stop) (original value), except bounded at 0 (thus, Math.max(0, string.length + stop)) as covered in the ECMA specification.

Source: Javascript: substr() v.s. substring()

Upvotes: 0

user2587132
user2587132

Reputation:

slice

.slice(start, end)

Does not include, the given end index element


substr

.substr(start,length)

Extracts from start position up to the no. of chars. specified


substring

.substring(start, end)

extracts the characters between the two specified indices


Upvotes: -1

Gabe
Gabe

Reputation: 86708

Even though it looks superficially like slice and substring do the same thing, the big difference is in how they handle negative arguments.

When JavaScript was first created in Netscape 2.0, there was just a substring method. If either of its arguments are negative, they are treated as 0.

When JavaScript 1.2 was introduced with Netscape 4.0, they wanted to add the behavior of allowing negative indexes to mean distances from the end of the string. They couldn't change substring to have this new behavior because it would break backward compatibility with scripts that expected negative indexes to be treated as 0, so they had to create a new function to support the added feature. This function was called slice, and was implemented on Array as well as String.

Another, smaller difference is that with substring the order of the arguments doesn't matter, so substring(1, 4) is the same as substring(4, 1). With slice, order does matter, so slice(4, 1) will just yield an empty string.

Upvotes: 8

JaredPar
JaredPar

Reputation: 754575

One item that makes them different is the second parameter that you have omitted

  • slice: the second parameter is the end index (exclusive) of the range to take.
  • substr: the second parameter is the length of the string to take from the index specified with the first parameter

Can you completely replicate the behavior of one method with the other on string instances? Yes. Why they chose to include both is probably lost to history. My guess though would be familiarity. I bet there are very few frameworks out there which have slice for strings but plenty that have substr.

Upvotes: 2

RRikesh
RRikesh

Reputation: 14381

In slice the arguments are the first index and last index. In substr, the arguments are first index and length.

Upvotes: 0

Carsten Massmann
Carsten Massmann

Reputation: 28196

Edit: Oooops - I was wrong - there IS a slice method for strings too! I will delete my post again - sorry for not researching properly!!! Or, well , may be not delete it, but leave this correction in it at least. ;-)

You are looking at two methods of different classes. substr can only be applied on String-objects while slice belongs to Array-objects. They might seem similar to yo, yet internally they work in different ways since the data they handle is different.

BTW, this is not a jQuery but a plain JavaScript question. ;-)

Upvotes: 1

Related Questions