js字符串常用方法

1、length:返回字符串的长度

const str = "Hello, World!";
console.log(str.length); // 输出 13

2、charAt(index):返回指定索引位置的字符

const str = "Hello, World!";
console.log(str.charAt(4)); // 输出 o

3、concat(str1, str2, ...):连接两个或多个字符串,并返回新的字符串

const str1 = "Hello";
const str2 = "World";
const str3 = str1.concat(", ", str2);
console.log(str3); // 输出 Hello, World

4、indexOf(substring, start):返回子字符串在原字符串中第一次出现的位置,如果未找到则返回-1

const str = "Hello, World!";
console.log(str.indexOf("World")); // 输出 7
console.log(str.indexOf("foo")); // 输出 -1
console.log(str.indexOf("Wor") !== -1); // true
// 位运算的方式还可以写成
console.log(~str.indexOf("Wor")); // -8
console.log(~str.indexOf("Wor1")); // 0

5、lastIndexOf(substring, start):返回子字符串在原字符串中最后一次出现的位置,如果未找到则返回-1

const str = "Hello, World!";
console.log(str.lastIndexOf("o")); // 输出 8
console.log(str.lastIndexOf("foo")); // 输出 -1

6、slice(start, end):从原字符串中提取指定范围的字符,并返回新的字符串(包前不包后)

const str = "Hello, World!";
console.log(str.slice(7, 12)); // 输出 World

7、substring(start, end):从原字符串中提取指定范围的字符,并返回新的字符串。与 slice() 类似,但不支持负数参数(包前不包后)

const str = "Hello, World!";
console.log(str.substring(7, 12)); // 输出 World
console.log(str.substring(2)); // 输出 llo, World!

8、substr(start, length):从原字符串中提取指定长度的字符,并返回新的字符串

const str = "Hello, World!";
console.log(str.substr(7, 5)); // 输出 World
console.log(str.substr(2, 3)); // 输出 llo

9、toLowerCase():将字符串转换为小写

const str = "Hello, World!";
console.log(str.toLowerCase()); // 输出 hello, world!

10、toUpperCase():将字符串转换为大写

const str = "Hello, World!";
console.log(str.toUpperCase()); // 输出 HELLO, WORLD!

11、trim():去除字符串两端的空格, 中间的空格不行

const str = "   Hello, World!   ";
console.log(str.trim()); // 输出 Hello, World!

12、split(separator):将字符串按照指定的分隔符分割为数组

const str = "Hello, World!";
const arr = str.split(", ");
console.log(arr); // 输出 ["Hello", "World!"]

13、replace(searchValue, replaceValue):将字符串中的指定内容替换为新的内容

const str = "Hello, World!";
const newStr = str.replace("World", "Universe");
console.log(newStr); // 输出 Hello, Universe!

14、startsWith(searchString, position):判断字符串是否以指定的子字符串开头

const str = "Hello, World!";
console.log(str.startsWith("Hello")); // 输出 true
console.log(str.startsWith("World")); // 输出 false

15、endsWith(searchString, position):判断字符串是否以指定的子字符串结尾

const str = "Hello, World!";
console.log(str.endsWith("World!")); // 输出 true
console.log(str.endsWith("Hello")); // 输出 false

16、includes(searchString, position):判断字符串是否包含指定的子字符串

const str = "Hello, World!";
console.log(str.includes("World")); // 输出 true
console.log(str.includes("foo")); // 输出 false

17、match(regexp):通过正则表达式在字符串中搜索匹配项,并返回匹配结果的数组

const str = "Hello, World!";
const matches = str.match(/[a-zA-Z]+/g);
console.log(matches); // 输出 ["Hello", "World"]

const str1 = "Hello,12131,a23,232,232 World!";
const matches1 = str1.match(/[a-zA-Z]+/g);
console.log(matches1); // 输出 ['Hello', 'a', 'World']

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值