substr
substr() 方法返回一个字符串中从指定位置开始到指定字符数的字符。
语法
str.substr(start[, length])
参数
- strat
开始提取字符的位置。如果为负值,则被看作 strLength + start,其中 strLength 为字符串的长度(例如,如果 start 为 -3,则被看作 strLength-3)。 - length
可选。提取的字符数。
substring
substring() 方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。
语法
str.substring(indexStart[, indexEnd])
参数
- indexStart
一个 0 到字符串长度之间的整数。 - indexEnd
可选。一个 0 到字符串长度之间的整数。
实例
var str = 'hello world';
console.log(str.substr(0)); //hello world
console.log(str.substr(0, 5)); //hello
console.log(str.substr(6, 5)); //world
var str = 'hello world';
console.log(str.substring(0)); //hello world
console.log(str.substring(0, 5)); //hello
console.log(str.substring(6)); //world