码迷,mamicode.com
首页 > 其他好文 > 详细

字符串的截取

时间:2019-12-14 15:45:21      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:字符串   长度   start   例子   方法   bst   定位   def   字符   

//方法返回一个字符串中从指定位置开始到指定字符数的字符
substr()
//语法
str.substr(start[, length])
//start
//开始提取字符的位置。如果为负值,则被看作 strLength + start,其中 strLength 为字符串的长度(例如,如果 start 为 -3,则被看作 strLength + (-3))。
//length
//可选。提取的字符数。

//例子:使用 substr

var str = "abcdefghij";
// (1,2): bc
console.log("(1,2): " + str.substr(1,2));
// (-3,2): hi
console.log("(-3,2): " + str.substr(-3,2));
// (-3): hij
console.log("(-3): " + str.substr(-3));
// (1): bcdefghij
console.log("(1): " + str.substr(1));
// (-20, 2): ab
console.log("(-20, 2): " + str.substr(-20,2));
// (20, 2): 无内容
console.log("(20, 2): " + str.substr(20,2));

字符串的截取

标签:字符串   长度   start   例子   方法   bst   定位   def   字符   

原文地址:https://www.cnblogs.com/notesblog/p/12039108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!