js操作字符串增删改查

不改变原字符串

增--concat()

let stringValue = "hello";
let result = stringValue.concat("world");
console.log(result); // "helloworld"
console.log(stringValue); // "hello"

删--slice()、subString()、substr()

let stringValue = "hello world";
//返回开始-结束(可省,默认到末尾)
console.log(stringValue.slice(2)); // "llo world" (3-末尾)
console.log(stringValue.slice(2, 7)); // "llo w"(3-6)
//返回开始-结束(可省,默认到末尾)
console.log(stringValue.substring(2)); // "llo world" (3-末尾)
console.log(stringValue.substring(2,7)); // "llo w" (3-6)
// substr()
console.log(stringValue.substr(2)); // "llo world"  (3-末尾)
console.log(stringValue.substr(2, 7)); // "llo worl" (从3开始的,7个元素)
console.log(stringValue); //hello world

trim() 、trimLeft()、trimRright()删除前后的所有空格符

let stringValue = " hello world ";
let trimStringValue = stringValue.trim();
console.log(stringValue); // " hello world "
console.log(trimStringValue); // "hello world"

repeat() 接收一个整数,表示复制次数

let stringValue = "hello ";
let copyStringValue = stringValue.repeat(2) // hello hello 
console.log(copyStringValue); //hello hello 
console.log(stringValue); //hello 

toLowerCase()、 toUpperCase() 大小写转换

let stringValue = "hello world";
console.log(stringValue.toUpperCase()); // "HELLO WORLD"
console.log(stringValue.toLowerCase()); // "hello world"

charAt() 返回给定索引位置的字符

let message = "hello";
console.log(message.charAt(1)); // "e"

indexOf() 返回字符第一位置,否则返回-1

let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4

startWith()、includes()  返回true or false

startWith() 只能从开头开始匹配

let message = "hello world";
console.log(message.startsWith("hel")); // true
console.log(message.startsWith("llo")); // false
console.log(message.startsWith("lle")); // false
console.log(message.includes("llo")); // true
console.log(message.includes("le")); // false

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值