1. indexOf()方法:返回字符串中指定字符或子字符串的位置,如果没有找到则返回-1。

例如:

let str = 'Hello World';
console.log(str.indexOf('o')); // 4
console.log(str.indexOf('l')); // 2
console.log(str.indexOf('x')); // -1
  1. substring()方法:返回字符串中指定起始位置和结束位置之间的子字符串。

例如:

let str = 'Hello World';
console.log(str.substring(0, 5)); // Hello
console.log(str.substring(6)); // World
  1. replace()方法:用一个新字符串替换原字符串中的指定字符或子字符串。

例如:

let str = 'Hello World';
console.log(str.replace('World', 'JavaScript')); // Hello JavaScript
  1. split()方法:将字符串按照指定分隔符分割成数组。

例如:

let str = 'a,b,c,d';
console.log(str.split(',')); // ['a', 'b', 'c', 'd']
  1. toUpperCase()方法和toLowerCase()方法:将字符串中的所有字母转换为大写或小写。

例如:

let str = 'Hello World';
console.log(str.toUpperCase()); // HELLO WORLD
console.log(str.toLowerCase()); // hello world
js常见字符串方法与使用方法举例说明

原文地址: https://www.cveoy.top/t/topic/bWZr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录