Coder Perfect

What is the distinction between substring and substr?

Problem

What is the distinction between the two?

alert("abc".substr(0,2));

and

alert("abc".substring(0,2));

Both appear to output “ab.”

Asked by Difference Engine

Solution #1

The distinction is found in the second argument. The index to stop at (but not include) is the second parameter to substring, whereas the maximum length to return is the second argument to substr.

Links?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

Answered by Delan Azabani

Solution #2

substr (MDN) accepts the following parameters: (from, length). substring (MDN) accepts the following parameters: (from, to).

MDN now examines the legacy of substr.

alert("abc".substr(1,2)); // returns "bc"
alert("abc".substring(1,2)); // returns "b"

You’ll recall that substring (with an I and another string extraction method, slice, both use indices (with an i).

When starting from 0 you can use either method.

Answered by Colin Hebert

Solution #3

There is another distinction, as yatima2975 mentioned in her response:

As an offset from the end of the string, substr() accepts a negative starting position. substring(), on the other hand, does not.

From MDN:

So, to summarize the functional distinctions:

Where begin-offset is 0 or more, substring(begin-offset, end-offset-exclusive)

begin-offset may also be negative in substr(begin-offset, length).

Answered by JefferMC

Solution #4

The fundamental distinction is that

substr() allows you to specify the maximum length to return

The indices can be specified with substring(), and the second parameter is NOT inclusive.

There are various differences between substr() and substring(), such as how equal and negative parameters are handled. It’s also worth noting that substring() and slice() are close but not identical.

  //*** length vs indices:
    "string".substring(2,4);  // "ri"   (start, end) indices / second value is NOT inclusive
    "string".substr(2,4);     // "ring" (start, length) length is the maximum length to return
    "string".slice(2,4);      // "ri"   (start, end) indices / second value is NOT inclusive

  //*** watch out for substring swap:
    "string".substring(3,2);  // "r"    (swaps the larger and the smaller number)
    "string".substr(3,2);     // "in"
    "string".slice(3,2);      // ""     (just returns "")

  //*** negative second argument:
    "string".substring(2,-4); // "st"   (converts negative numbers to 0, then swaps first and second position)
    "string".substr(2,-4);    // ""
    "string".slice(2,-4);     // ""

  //*** negative first argument:
    "string".substring(-3);   // "string"        
    "string".substr(-3);      // "ing"  (read from end of string)
    "string".slice(-3);       // "ing"        

Answered by Nate Lipp

Solution #5

Another quirk I recently discovered is that “abcd” is not supported in Internet Explorer 8. substr(-1) incorrectly returns “abcd,” whereas Firefox 3.6 correctly returns “d.” On both, slice works properly.

More information about this subject can be found here.

Answered by yatima2975

Post is based on https://stackoverflow.com/questions/3745515/what-is-the-difference-between-substr-and-substring