您当前的位置: 首页 > 

彭世瑜

暂无认证

  • 2浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

js字符串截取substring和substr

彭世瑜 发布时间:2019-10-31 14:11:43 ,浏览量:2

定义测试使用的字符串

var text = "Hello World";

字符串Hello World 字符对应下标

HelloWorld012345678910

字符串截取

// substring(start, end) 截取范围:[start, end)
text.substring(6, 11)
"World"

// substr(start, length) 
text.substr(6, 5)
"World"

字符串查找

// 从前向后
text.indexOf("o")
4

// 从后向前
text.lastIndexOf("o")
7

字符串和数组转换

// 字符串转数组
text.split(" ")
["Hello", "World"]

// 数组转字符串
["Hello", "World"].join("-")
"Hello-World"

参考 JS截取字符串方法实例

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.2972s