Js-字符串接口

一:字符串的分割 str.split('');

let str = "abcd";
let res1 = str.split('');//[ 'a', 'b', 'c', 'd' ]
let res2 = str.split('c');//[ 'ab', 'd' ]

二:字符串的截取 substring、substr、slice

1.str.slice(start,end);//end位置不会截取

let str = "abcdef";
let res1 = str.slice(2,4);//cd
let res2 = str.slice(3);//def

2.str.substring(start,end);//开始位置,结束位置

let str = "abcdef";
let res1 = str.substring(2,3);//c
let res2 = str.substring(3);//def

3.str.substr(start,count);//开始位置,截取数量

let str = "abcdef";
let res1 = str.substr(2,3);//cde
let res2 = str.substr(2);//cdef

三:字符串的替换 str.splace('a','b');//a可以使用正则表达式

let str = "ab_夏夜_cdefa";
let res1 = str.replace('c','哈哈');//ab_夏夜_哈哈defa
let res2 = str.replace(/([\u4E00-\u9FA5])+/g,'xiaye');//ab_xiaye_cdefa

 

四:字符串大小写转换 toUpper

let str1 = "abc";
let str2 = "EDF";
let res1 = str1.toUpperCase();//ABC
let res2 = str2.toLowerCase();//edf

五:字符串去前后空格

let str = "    abc edf    ";
let res = str.trim();//abc edf

六:其它

1.字符串的拼接

let str = 'abc' + 'edf';
let str1 = `abc${str}`;//abcabcedf

2.字符串匹配 str.match(str | regexp);//可以是字符串,也是是正则,字符置灰匹配一个,而正则如果/g可以全局匹配

let str = 'abcdheheefj';
let res1 = str.match('he');//['he']
let res2 = str.match(/he/g);//['he','he']

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值