Hi Rashmi,
The substring() method extracts the characters from a string( between two specified indices), returns the new sub string.
This method extracts the characters in a string between "start" and "end" indices ,"end" index optional.
If "start" is greater than "end", this method will swap the two arguments, i.e. str.substring(1,4) == str.substring(4,1).
If either "start" or "stop" is less than 0, it is treated as if it were 0.
Syntax:
string.substring(start,end)
Parameter Description
start=> Required. The position where to start the extraction. First character is at index 0
end=> Optional. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the string
Example 1:
var str = "Hello world!";
var res = str.substring(1, 4);
Out Put:
ell
Example 2:
var str = "Hello world!";
var res = str.substring(2);
Out Put:
llo world!
Example 3:
var str = "Hello world!";
var res = str.substring(-4);
Out Put:
Hello world!
Example 4:
var str = "Hello world!";
var res = str.substring(4, 1);
Out Put:
ell
Posted On:
21-Jun-2015 08:14