There are 3 methods for extracting a part of a string:
slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: the starting index (position), and the ending index (position).
This example slices out a portion of a string from position 7 to position 13:
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var res = str.slice(7,13); console.log(res);
If a parameter is negative, the position is counted from the end of the string.
This example slices out a portion of a string from position -12 to position -6:
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var res = str.slice(-12,-6); console.log(res);
If you omit the second parameter, the method will slice out the rest of the string:
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var res = str.slice(7); console.log(res);
or, counting from the end:
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var res = str.slice(-12) console.log(res);
substring() is similar to slice().
The difference is that substring() cannot accept negative indexes.
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var res = str.substring(7,13); console.log(res);
If you omit the second parameter, substring() will slice out the rest of the string.
In the substr() method the second parameter specifies the length of the extracted part.
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var res = str.substr(7,6); console.log(res);
If the first parameter is negative, the position counts from the end of the string.
The second parameter can not be negative, since it defines the length.
If you omit the second parameter, substr() will slice out the rest of the string.