這個方法在一個字符串返回字符開始於通過指定的字符數的指定位置。
語法
string.substr(start[, length]);
下面是參數的詳細信息:
注意:如果start 是負數,substr 使用它作為從字符串的末尾字符索引
返回值:
例子:
<html> <head> <title>JavaScript String substr() Method</title> </head> <body> <script type="text/javascript"> var str = "Apples are round, and apples are juicy."; document.write("(1,2): " + str.substr(1,2)); document.write("<br />(-2,2): " + str.substr(-2,2)); document.write("<br />(1): " + str.substr(1)); document.write("<br />(-20, 2): " + str.substr(-20,2)); document.write("<br />(20, 2): " + str.substr(20,2)); </script> </body> </html>
這將產生以下結果:
(1,2): pp (-2,2): Ap (1): pples are round, and apples are juicy. (-20, 2): Ap (20, 2): d