Javascript String substr()
gets a sub string from a longer string.
The substr()
method can accept one or two arguments.
str.substr(start[, length])
If the second argument is omitted, it is assumed that the ending position is the length of the string.
The substr()
method does not alter the string itself.
It returns a string value as the result, leaving the original unchanged.
let stringValue = "hello world"; console.log(stringValue.substr(3)); // "lo world" console.log(stringValue.substr(3, 7)); // "lo worl"
For the substr()
method, a negative first argument is treated as the length of the string plus the number, whereas a negative second number is converted to 0.
let stringValue = "hello world"; console.log(stringValue.substr(-3)); // "rld" console.log(stringValue.substr(3, -4)); // "" (empty string)