What is the difference between substr and substring?

Asked

Viewed 1,237 times

18

I want to know the difference between

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

and

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

Both appear to produce "ab".

  • 2

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

5 answers

20


substr() you go from the initial position indicated by the first argument to the amount of characters indicated by the second argument, that is to say the second argument is a length of the portion you wish to take.

substring() you go from the initial position indicated by the first argument to the final position indicated by the second argument, therefore determines the stretch that should take, ie it works as a index.

You should use each one with the most appropriate information you have at the moment, or the amount of characters you know you need or the beginning and end of the text part you need.

With numbers showing positions it is easier to view:

console.log("012345".substr(1, 3));
console.log("012345".substring(1, 3));

Note that the first example took 3 characters from the second character, and another example taken from the same place and stopped at the third character, so it only showed 2 of them. There are cases that can really work:

console.log("012345".substr(0, 3));
console.log("012345".substring(0, 3));

I put in the Github for future reference.

  • @Máttheusspoo Thanks for warning. I already made the exchange.

10

The difference is that the replace works with quantities of characters (specified or not in the second parameter) from a position index (index) of the string (specified in the first parameter), while substring works exclusively with indexes in the first and second parameters.

For example:

var string = "abc";

string.substr(0,2) // retorna "ab": caractere no índice 0 + 1 caractere
                   // (totalizando 2 caracteres)

string.substring(0,2) // retorna "ab": índice 0 até a posição 2 ("c"),
                      // mas a posição especificada no segundo parâmetro não
                      // é incluída no retorno, retornando apenas "ab"

In short:

The replace returns the amount of characters entered in the second parameter from the position given in the first (if the second parameter is omitted, it will return to the end of the string).

The substring will return from the position given in the first parameter to the position given in the second, however the position character in the second parameter is not included (i.e., goes to the previous character). As in the replace, if the second parameter is omitted, it will return all characters from the given initial position until the end of the string.

See in the example below with substring(1,3):

var string = "abcdef";
console.log(string.substring(1,3)); // retorna "bc"

Returns from position 1 (index 1) to position 3 (letter "d"), but a letter "d" is not included in the return.

Already with replace will return "Bcd", that is, 3 characters from index 1 (position 1 + 2 characters below):

var string = "abcdef";
console.log(string.substr(1,3)); // retorna "bcd"

Then you can ask: Which one should I use?

It depends on what you want to return. If you want to return an X number of characters from a position, you would use replace, but if you want to return characters from a position to a position before an occurrence, you should use substring:

var string = "abcdef";
console.log(string.substring(0, string.indexOf("d"))); // retorna "abc"

Docs:

7

Since you decided for some reason to copy Soen’s question, I will give you the same answer that is in the post.

The difference is in the second argument. The second substring argument is the index where it will stop (not included), but the second substr statement is the maximum size that the function will return.

Links?

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substr

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring

(translated from: https://stackoverflow.com/questions/3745515/what-is-the-difference-between-substr-and-substring)

5

Just one more example to leave registered here on the site:

let texto = 'Testando JAVASCRIPT';
let texSu = texto.substring(9,19);  // pega a string a partir do 9º caractere até o 18º caractere mais um
let texSs = texto.substr(9,10);  // pega a string a partir do 9º caractere mais 10 caracteres

console.log(texto.length);
console.log(texSu);
console.log(texSs);

4

The second parameter of substring() is to terminate the string and not include in the case the specified position character.

The second parameter of substr() specifies the maximum size to be returned.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.