目录

substr()

此方法返回从指定位置开始的字符串中的字符到指定的字符数。

语法 (Syntax)

string.substr(start[, length]); 

参数细节 (Argument Details)

  • start - 开始提取字符的位置(0到1之间的整数,小于字符串的长度)。

  • length - 要提取的字符数

返回值 (Return Value)

substr()方法根据给定的参数返回新的子字符串。

例子 (Example)

var str = "Apples are round, and apples are juicy."; 
console.log("(1,2): "    + str.substr(1,2)); 
console.log("(-2,2): "   + str.substr(-2,2)); 
console.log("(1): "      + str.substr(1)); 
console.log("(-20, 2): " + str.substr(-20,2)); 
console.log("(20, 2): "  + str.substr(20,2));    

输出 (Output)

(1,2): pp 
(-2,2): y. 
(1): pples are round, and apples are juicy. 
(-20, 2): nd 
(20, 2): d  
↑回到顶部↑
WIKI教程 @2018