Extract characters from a string:
str.substring(1, 4) // Returns "ell"
More "Try it Yourself" examples below.
Definition and Usage
substring() method extracts characters, between to indices (positions), from a string,
and returns the substring.
substring() method extracts characters between "start" and "end", not
If "start" is greater than "end",
substring() will swap the two arguments,
meaning (1, 4) equals (4, 1).
If "start" or "end" is less than 0, they are treated as 0.
substring() method does not change the original string.
It is fully supported in all browsers:
|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|
|Return Value:||A new String containing the extracted characters|
Begin the extraction at position 2, and extract the rest of the string:
If "start" is greater than "end", it will swap the two arguments:
If "start" is less than 0, it will start extraction from index position 0:
Extract only the first character:
Extract only the last character: