字符串常用操作:截取

str.substring(start,end)//截取下标start到end-1之间的字符串(包含start不包含end)

.end可选,默认从start到结束

.start>end,参数会自动调换位置

.start为负数,等价于start=0 

var str='0123456789';
console.log( str.substring(1) );//123456789
console.log( str.substring(1,5));//1234
console.log( str.substring(3,1) );//12
console.log( str.substring(-2));//0123456789
console.log( str.substring(1,-2) );//首先会互换位置(-2,1),等价于(0,1),结果为0

str.substr(start,length)//从指定下标start开始截取length个数

.lenght可选,默认从start到结束

.start为负数,则从后往前截取

var str='0123456789';
console.log( str.substr(1) );//123456789
console.log( str.substr(1,5));//12345
console.log( str.substr(3,1) );//3
console.log( str.substr(-2));//89

str.slice(start,end)//从指定下标start开始截取到指定下标end包含start不包含end)

.end可选,默认从start截到结束,可为负数

.start需小于end

.start为负数,则从后往前数

var str='0123456789';
console.log( str.slice(1) );//123456789
console.log( str.slice(1,5));/1234
console.log( str.slice(3,1) );//空
console.log( str.slice(-2));//89
console.log( str.slice(1,-2) );//1234567
console.log( str.slice(-5,-2) );//567

str.split(string,limit)//把字符串在string字符(串)处分割,并返回数组

 .string可为字符串或正则表达式

.string不填,等价于str转成数组,一个元素

.string=“”,等价于str转成数组,多个元素

.limit可选,返回limit个元素的数组

var str='/http:www/';
console.log( str.split());//["/http:www/"]
console.log( str.split("") );//["/", "h", "t", "t", "p", ":", "w", "w", "w", "/"]
console.log( str.split("/"));//["", "http:www", ""]
console.log( str.split("w"));//["/http:", "", "", "/"]
console.log( str.split("w",1));//["/http:"]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值