Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between String.slice and String.substring?

Does anyone know what the difference is between these two methods?

String.prototype.slice String.prototype.substring 
like image 538
tmim Avatar asked Feb 11 '10 10:02

tmim


People also ask

What's the difference between Slice and substring?

A big difference with substring() is that if the 1st argument is greater than the 2nd argument, substring() will swap them. slice() returns an empty string if the 1st argument is greater than the 2nd argument.

What is difference between string and substring?

a substring is a subsequence of a string in which the characters must be drawn from contiguous positions in the string. For example the string CATCGA, the subsequence ATCG is a substring but the subsequence CTCA is not.

What is string slice method?

The slice() method extracts a part of a string. The slice() method returns the extracted part in a new string. The slice() method does not change the original string. The start and end parameters specifies the part of the string to extract.

What does string slice return?

slice() The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.


2 Answers

slice() works like substring() with a few different behaviors.

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

What they have in common:

  1. If start equals stop: returns an empty string
  2. If stop is omitted: extracts characters to the end 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 start is negative: sets char from the end of string, exactly like substr() in Firefox. This behavior is observed in both Firefox and IE.
  3. If 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: Rudimentary Art of Programming & Development: Javascript: substr() v.s. substring()

like image 56
Daniel Vassallo Avatar answered Oct 22 '22 02:10

Daniel Vassallo


TL;DR;

  • If you know the index (the position) on which you'll stop (but NOT include), use slice().
  • If you know the length of characters to be extracted, use substr().

Otherwise, read on for a full comparison

Syntax

  • string.slice(start,end)
  • string.substr(start,length)
  • string.substring(start,end)

Note #1: slice()==substring()

What it does?

  • The slice() method extracts parts of a string and returns the extracted parts in a new string.
  • The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
  • The substring() method extracts parts of a string and returns the extracted parts in a new string.

Note #2: slice()==substring()

Changes the Original String?

  • slice() Doesn't
  • substr() Doesn't
  • substring() Doesn't Note #3: slice()==substring()

Using Negative Numbers as an Argument

  • slice() selects characters starting from the end of the string
  • substr()selects characters starting from the end of the string
  • substring() Doesn't Perform

Note #3: slice()==substr()

If the First Argument is Greater than the Second

  • slice() Doesn't Perform
  • substr() since the Second Argument is NOT a position, but length value, it will perform as usual, with no problems
  • substring() will swap the two arguments, and perform as usual

The First Argument

  • slice() Required, indicates: Starting Index
  • substr() Required, indicates: Starting Index
  • substring() Required, indicates: Starting Index

Note #4: slice()==substr()==substring()

The Second Argument

  • slice() Optional, The position (up to, but not including) where to end the extraction
  • substr() Optional, The number of characters to extract
  • substring() Optional, The position (up to, but not including) where to end the extraction

Note #5: slice()==substring()

What if the Second Argument is Omitted?

  • slice() selects all characters from the start-position to the end of the string
  • substr() selects all characters from the start-position to the end of the string
  • substring() selects all characters from the start-position to the end of the string

Note #6: slice()==substr()==substring()

So, you can say that there's a difference between slice() and substr(), while substring() is basically a copy of slice().

like image 25
Waddah Avatar answered Oct 22 '22 01:10

Waddah