JS substr Function

substr() is a String prototype, it returns a sub string.

String.substr(start [, length]): if the length is not specified, it will subtract from the start position to the end of the string.

var str = "javascript endmemo";
alert(str.substr(5)); //cript endmemo
alert(str.substr(5,10)); //cript endm

If the start position < 0, it will start from the end:

var str = "javascript endmemo";
alert(str.substr(-5)); //dmemo

substr() supports unicode:

var str = "中国人民银行";
alert(str.substr(-5)); //国人民银行
alert(str.substr(2,4)); //人民银行

endmemo.com © 2024  | Terms of Use | Privacy | Home