JavaScript substr() with Example

The substr() method extracts the characters from a string, beginning at “start” and through the specified number of character, and returns the new sub string.

Syntax
string.substr(start,length)

Start – Required. The index where to start the extraction. First character is at index 0.
Length- Optional. The number of characters to extract. If omitted, it extracts the rest of the string.

In the example
document.write(str.substr(1)+”<br />”);
First character of “Welcome to www.ispsd.Com” w is removed as seen in output.

document.write(str.substr(3,9));
First 3 characters of “Welcome to www.ispsd.Com” wel will be removed and after 9 characters rest of the words will be omitted too.

CODE









OUTPUT

    elcome to www.ispsd.Com
    come to w

Leave a Reply