JS-字符串处理

更多字符串用法:http://www.w3school.com.cn/jsref/jsref_obj_string.asp

1.字符串截取

const str = '123qwe'
console.log('slice<<<'+str.slice(1, 3))//1表示开始位置,3表示结束位置的下一位置
console.log('substring<<<'+str.substring(1, 3))//同上
console.log('substr<<<'+str.substr(1, 3))//1表示开始位置,3表示截取长度
slice<<<23
substring<<<23
substr<<<23q

详细使用参考:js字符串截取函数slice()、substring()、substr()

2.判断字符串是否包含另一字符串

const str = '123qwe'
console.log('包含<<<'+str.indexOf('3q'))//返回字符串首位所在的位置
console.log('不包含<<<'+str.indexOf('33'))//返回-1
包含<<<2
不包含<<<-1

3.根据某字符切割成数组

const str2 = 'How are you'
console.log('分割///'+str2.split(' '))
console.log('分割///'+str2.split('')) 
console.log('分割///'+str2.split(' ',2))
分割///How,are,you
分割///H,o,w, ,a,r,e, ,y,o,u
分割///How,are

4.截取字符串

const str2 = 'How are you'
console.log('分割///'+str2.slice(5))
console.log('分割///'+str2.slice(5,9)) 
console.log('分割///'+str2.slice(-1))
分割///re you
分割///re y
分割///u

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值