JavaScript String substring() Method | javascript substring indexof
ExamplesExtractasubstringfromtext:lettext="Helloworld!";letresult=text.substring(1,4);TryitYourself»[1]Startfromposition2:letresult=text.substring(2);TryitYourself»[2]Moreexamplesbelow.DescriptionThesubstring()methodextractscharacters,betweentwoindices(positions),fromastring,andreturnsthesubstring.Thesubstring()methodextractscharactersfromstarttoend(exclusive).Thesubstring()methoddoesnotchangetheoriginalstring.Ifstartisgreaterthanend,argumentsareswapped:(4,1)=(1,4).Startorendvalueslessthan0,...
ExamplesExtract a substring from text:
let text = "Hello world!";let result = text.substring(1, 4);
Try it Yourself »[1]Start from position 2:
let result = text.substring(2);
Try it Yourself »[2]More examples below.
DescriptionThe substring() method extracts characters, between two indices (positions), from a string, and returns the substring.
The substring() method extracts characters from start to end (exclusive).
The substring() method does not change the original string.
If start is greater than end, arguments are swapped: (4, 1) = (1, 4).
Start or end values less than 0, are treated as 0.
Syntaxstring.substring(start, end)
Parameters Parameter Description start Required.Start position.First character is at index 0. end Optional.End position (up to, but not including).If omitted: the rest of the string. Return Value Type Description A stringA string containing the ...