1.replace 和 replaceAll
replace()
替换字符串中的部分内容,返回一个新字符串,原始的字符串不会改变。
语法:replace(pattern, replacement)
参数:
pattern
:需要替换的内容,可以是字符串或者一个带有 Symbol.replace
方法的对象,典型的例子就是正则表达式。
replacement
:可以是字符串或函数。如果是字符串,它将替换由 pattern
匹配的子字符串;如果是函数,将为每个匹配调用该函数,并将其返回值用作替换文本。
示例:
let str = "hello,hello"; let newStr = str.replace("h","H") console.log(newStr) // Hello,hello // 注意:如果使用正则表达式并带 g 标志,可以全局替换。 let newStr2 = str.replace(/h/g,"H") console.log(newStr2) // Hello,Hello
replaceAll()
同上,替换所有满足pattern
的部分,返回一个新字符串。
示例:
let str = "hello,hello"; let newStr = str.replaceAll("h","H") console.log(newStr) // Hello,Hello
2.includes
判断字符串是否包含指定的子字符串,返回布尔值。
语法:includes(searchString)
includes(searchString, position)
参数:
searchString
:一个要在 str
中查找的字符串。如果该参数被省略或传入 undefined
,includes()
方法会在字符串中搜索 "undefined"
。
position(选填)
:在字符串中开始搜索 searchString
的位置。默认值为 0
。
示例:
let str = "hello,hello"; let hasWorld = str.includes("hello") // true
3.split
将字符串按某种模式
分割成一个数组,并返回该数组。
语法:split(separator, limit)
参数:
separator
:分隔符,即按照separator这种模式的分隔符进行分割,可以为undefined
limit(可选)
:一个非负整数,指定数组中包含的子字符串的数量限制。
示例:
const str = "Hello,World!"; const arr = str.split(","); const arr2 = str.split(""); console.log(arr); // 输出: ['Hello', 'World!'] console.log(arr2); // 输出: ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
4.slice
从指定的 indexStart
开始提取到 indexEnd
(不包含)的子字符串。如果省略 indexEnd
,则提取到字符串末尾。可以使用负数索引从字符串末尾开始计数。
语法:slice(indexStart, indexEnd)
参数:
indexStart
:要返回的子字符串中包含的第一个字符的索引(索引从0开始)。
indexEnd(可选)
:要返回的子字符串中最后一个字符的索引。
示例:
const str = "Hello,World!"; console.log(str.slice(0, 5)); // 输出: Hello console.log(str.slice(6)); // 输出: World! console.log(str.slice(-6)); // 输出: World!
5.substring
提取指定字符串,与 slice
类似,但不支持负数索引。如果 indexStart
大于 indexEnd
,它会自动交换两个参数。
const str = "Hello,World!"; console.log(str.substring(0, 5)); // 输出: Hello console.log(str.substring(6)); // 输出: World!
6.indexOf
返回 searchString
在字符串中第一次出现的索引位置(从0开始),如果未找到则返回 -1。startIndex
是可选的,表示开始查找的位置。
语法:indexOf(searchString, startIndex)
参数:
searchString
:要搜索的子字符串。所有传入值都会被强制转换为字符串,因此如果该参数被省略或传入 undefined
,indexOf()
方法会在字符串中搜索 "undefined"
。
startIndex(可选)
:开始查找字符串的位置,默认为 0
。如果 startIndex
大于调用字符串的长度,则该方法根本不搜索调用字符串。如果 startIndex
小于零,该方法的行为就像 startIndex
为 0
时一样。
示例:
const str = "Hello,World!"; console.log(str.indexOf("l")); // 输出: 2 console.log(str.indexOf("abc")); // 输出: -1
7.startsWith
用来判断当前字符串是否以另外一个给定的子字符串开头,返回一个布尔值。
语法:startsWith(searchString, startIndex)
参数:
searchString
:要在该字符串开头搜索的子串。
startIndex(可选)
:searchString
期望被找到的起始位置(即 searchString
的第一个字符的索引)。默认为 0
。
示例:
const str = "Hello,World!"; console.log(str.startsWith("Hello")); // 输出: true console.log(str.startsWith("H")); // 输出: true console.log(str.startsWith("H",1)); // 输出: false
8.endsWith
同上,endsWith(searchValue, length)
返回一个布尔值,表示字符串是否以 searchValue
结尾。length
是可选的,表示检查的字符串长度。
示例:
const str = "Hello,World!"; console.log(str.endsWith("World!")); // 输出: true
9.toUpperCase 和 toLowerCase
toUpperCase()
:将字符串转换为大写。
const str = "Hello, World!"; console.log(str.toUpperCase()); // 输出: HELLO, WORLD!
toLowerCase()
:将字符串转换为小写。
const str = "Hello, World!"; console.log(str.toLowerCase()); // 输出: hello, world!
10.padStart
用另一个字符串填充当前字符串(如果需要会重复填充),直到达到给定的长度。填充是从当前字符串的开头开始的。
语法:padStart(targetLength, padString)
参数:
targetLength
:当前 str
填充后的长度。如果该值小于或等于 str.length
,则会直接返回当前 str
。
padString(可选)
:用于填充当前 str
的字符串。如果 padString
太长,无法适应 targetLength
,则会从末尾被截断。默认值为 Unicode“空格”字符(U+0020)。
const str = "8" console.log(str.padStart(2,'0')) // 输出: 08 console.log(str.padStart(8,'01')) // 输出: 01010108
11.trim 和 trimEnd
trim()
方法会从字符串的两端移除空白字符,并返回一个新的字符串,而不会修改原始字符串。
const str = " Hello world! "; console.log(str.trim()) // 输出:"Hello world!"
trimEnd()
方法会从字符串的结尾移除空白字符,并返回一个新的字符串,而不会修改原始字符串。
const str = " Hello world! "; console.log(str.trimEnd()) // 输出:" Hello world!"
12.repeat
指定字符串重复出现的次数。
语法:repeat(count)
参数:
count
:介于 0
和 +Infinity
之间的整数。表示在新构造的字符串中重复了多少遍原字符串。
示例:
const str = "Happy!" console.log(str.repeat(2)) // 输出:Happy!Happy! console.log(str.repeat(0)) // 输出:'' console.log(str.repeat(2.8)) // 输出:Happy!Happy!
13.charAt
返回指定索位置的字符。
语法:charAt(index)
参数:
index
:要返回字符的索引,从零开始。会被转换为整数,undefined
会被转换为 0。
示例:
const str = "Hello World!" console.log(str.charAt(1)) // 输出:e console.log(str.charAt()) // 输出:H
14.concat
连接字符串(合并字符串),并返回一个新的字符串。
语法:concat(str1)
、concat(str1,str2)
、concat(str1, str2, /* …, */ strN)
参数:需要合并的字符串
示例:
const str1 = 'Hello' const str2 = "World" const str3 = "!" console.log(str1.concat(str2)) // 输出:HelloWorld console.log(str1.concat(str2,str3)) // 输出:HelloWorld!