js字符串操作方法

字符串操作
slice substring substr substr split join String(1234) toString 都不会改变原字符
1.slice(start, end)

2.substring(start, end)

3.substr(start, number) // number是个数

4.split(’,’) // 根据, 拆分为数组

5.join(’’) // 数组转字符

6.String(1234)\

toString

7.concat
var stringValue = "hello ";
var result = stringValue.concat(“world”, “!”);
alert(result); //“hello world!”
alert(stringValue); //“hello”

8.item.toUpperCase() //转大写
item.toLowerCase() //转小写

9.charAt(index)
var str=“hello world”;
console.log(str.charAt(1));//e
console.log(str.charCodeAt(1))

str.charCodeAt(3) //index为3的Unicode 值,返回108
String.fromCharCode(108) // l

10.indexof
var str=“hello world”;
console.log(str.indexOf(“o”));//4
console.log(str.lastIndexOf(“o”));//7
console.log(str.indexOf(“o”,6));//7
console.log(str.lastIndexOf(“o”,6));//4’

str.includes(‘h’) // true
in

11.去除空格 str.trim()
var str=" hello world ";
console.log(’(’+str.trim()+’)’);//(hello world)
12.replace(原字符串一部分,要替换的部分)

    var str="cat,bat,sat,fat";
    var res=str.replace("at","one");//第一个参数是字符串,所以只会替换第一个子字符串
    console.log(res);//cone,bat,sat,fat

    var res1=str.replace(/at/g,"one");//第一个参数是正则表达式,所以会替换所有的子字符串
    console.log(res1);//cone,bone,sone,fone

13.search
var str = “ABCDECDF”;
str.search(“CD”); // 或 str.search(/CD/i);
结果:2

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端段

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值