JavaScript String substr() Method | Js substr
ExamplesExtractasubstringfromtext:lettext="Helloworld!";letresult=text.substr(1,4);TryitYourself»[1]Startatposition2:letresult=text.substr(2);TryitYourself»[2]Moreexamplesbelow.DescriptionThesubstr()methodextractsapartofastring.Thesubstr()methodbeginsataspecifiedposition,andreturnsaspecifiednumberofcharacters.Thesubstr()methoddoesnotchangetheoriginalstring.Toextractcharactersfromtheendofthestring,useanegativestartposition.Syntaxstring.substr(start,length)ParametersParameterDescriptionstartRe...
ExamplesExtract a substring from text:
let text = "Hello world!";let result = text.substr(1, 4);
Try it Yourself »[1]Start at position 2:
let result = text.substr(2);
Try it Yourself »[2]More examples below.
DescriptionThe substr() method extracts a part of a string.
The substr() method begins at a specified position, and returns a specified number of characters.
The substr() method does not change the original string.
To extract characters from the end of the string, use a negative start position.
Syntaxstring.substr(start, length)
Parameters Parameter Description start Required.The start position.First character is at index 0.If start is greater than the length, substr() returns "".If start is negative, substr() counts from the end of the string.
length Optional.The number of characters to extract.If omitted, it extracts the rest of the string Return Value Type D...