What is the difference between substr and substring?


I want to know the difference between




Both seem to produce "ab".

asked by anonymous 31.10.2018 / 20:54

5 answers


substr() you go from the indicated starting position by the first argument up to the number of characters indicated by the second argument, ie the second argument is a length of the portion you want to pick.

substring() you go from the indicated starting position by the first argument to the final end position indicated by the second argument, so it determines the excerpt that it should take, ie it works as an index

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

With numbers showing positions, it's easier to see:

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 took from the same place and stopped at the third character, so only showed 2 of them. There are cases that can give the same result:

console.log("012345".substr(0, 3));
console.log("012345".substring(0, 3));
31.10.2018 / 20:59

The difference is that substr works with character quantities (specified or not in the second parameter) from an index (index) position of the string (specified in first parameter), whereas 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 substr returns the amount of characters entered in the second parameter from the position entered 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 entered in the first parameter to the position reported in the second, however, the character of the position in the second parameter is not included (ie goes to the previous character ). As in substr , if you omit the second parameter, it will return all characters from the initial start position to the end of the string.

See 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 the   letter "d" is not included in the return.

With substr 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 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 substr , but if you want to return characters from a position to a position before an occurrence, you must use substring :

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


31.10.2018 / 21:13

As you have decided for some reason to copy the SOen question, I will give you the same answer as 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 argument is the maximum size that the function will return.




(translated from: link )

31.10.2018 / 21:00

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

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

31.10.2018 / 21:01

Just another example to register 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

31.10.2018 / 21:13