- 1.String.fromCharCode()
- 2.String.prototype.length
- 3.String.prototype.charAt()
- 4.String.prototype.charCodeAt()
- 5.String.prototype.concat()
- 6.String.prototype.slice()
- 7.String.prototype.substring()
- 8.String.prototype.substr()
- 9.String.prototype.indexOf(),String.prototype.lastIndexOf()
- 10.String.prototype.trim()
- 11.String.prototype.toLowerCase(),String.prototype.toUpperCase()
- 12.String.prototype.match()
- 13.String.prototype.search(),String.prototype.replace()
- 14.String.prototype.split()
- 15.String.prototype.localeCompare()
- 16.repeat()
- 17.String.raw()
- 18.includes(), startsWith(), endsWith()
- 19.padStart(),padEnd()
- 20.trimStart(),trimEnd()
- 21.matchAll()
- 22.replaceAll()
1.String.fromCharCode()
String对象提供的静态方法(即定义在对象本身,而不是定义在对象实例的方法),主要是String.fromCharCode()。该方法的参数是一个或多个数值,代表 Unicode 码点,返回值是这些码点组成的字符串。
String.fromCharCode(104, 101, 108, 108, 111)
// "hello"
2.String.prototype.length
字符串实例的length属性返回字符串的长度。
'abc'.length // 3
3.String.prototype.charAt()
charAt方法返回指定位置的字符,参数是从0开始编号的位置。
let s = new String('abc');
s.charAt(); a
console.log(s)
// String {"abc"}
4.String.prototype.charCodeAt()
charCodeAt()方法返回字符串指定位置的 Unicode 码点(十进制表示),相当于String.fromCharCode()的逆操作。
'abc'.charCodeAt(1) // 98
5.String.prototype.concat()
concat方法用于连接两个字符串,返回一个新字符串,不改变原字符串。
'abc'.concat('bcd')
// "abcbcd"
'abc'.concat('bcd','dss')
// "abcbcddss"
'abc'.concat(1,2,3)
// "abc123"
6.String.prototype.slice()
slice()方法用于从原字符串取出子字符串并返回,不改变原字符串。它的第一个参数是子字符串的开始位置,第二个参数是子字符串的结束位置(不含该位置)。
'JavaScript'.slice(0, 4) // "Java"
'JavaScript'.slice(4) // "Script"
7.String.prototype.substring()
substring方法用于从原字符串取出子字符串并返回,不改变原字符串,跟slice方法很相像。它的第一个参数表示子字符串的开始位置,第二个位置表示结束位置(返回结果不含该位置)
'Javascript'.substring(0,4) // "Java"
8.String.prototype.substr()
substr方法用于从原字符串取出子字符串并返回,不改变原字符串,跟slice和substring方法的作用相同。
substr方法的第一个参数是子字符串的开始位置(从0开始计算),第二个参数是子字符串的长度。
'Javascript'.substr(0,4) // "Java"
9.String.prototype.indexOf(),String.prototype.lastIndexOf()
indexOf方法用于确定一个字符串在另一个字符串中第一次出现的位置,返回结果是匹配开始的位置。如果返回-1,就表示不匹配。
indexOf方法还可以接受第二个参数,表示从该位置开始向后匹配。
'hello'.indexOf('o') // 4
'hello'.indexOf('h',1) // -1
lastIndexOf方法的用法跟indexOf方法一致,主要的区别是lastIndexOf从尾部开始匹配,indexOf则是从头部开始匹配。
'hello world'.lastIndexOf('o') // 7
10.String.prototype.trim()
trim方法用于去除字符串两端的空格,返回一个新字符串,不改变原字符串。
该方法去除的不仅是空格,还包括制表符(\t、\v)、换行符(\n)和回车符(\r)。
' hello world '.trim()
// "hello world"
'\r\nabc \t'.trim() // 'abc'
11.String.prototype.toLowerCase(),String.prototype.toUpperCase()
toLowerCase方法用于将一个字符串全部转为小写,toUpperCase则是全部转为大写。它们都返回一个新字符串,不改变原字符串。
'Hello World'.toLowerCase()
// "hello world"
'Hello World'.toUpperCase()
// "HELLO WORLD"
12.String.prototype.match()
match方法用于确定原字符串是否匹配某个子字符串,返回一个数组,成员为匹配的第一个字符串。如果没有找到匹配,则返回null。
'hello'.match('ll')
// ["ll", index: 2, input: "hello", groups: undefined]
'hello'.match(';;')
// null
返回的数组还有index属性和input属性,分别表示匹配字符串开始的位置和原始字符串。
13.String.prototype.search(),String.prototype.replace()
search方法的用法基本等同于match,但是返回值为匹配的第一个位置。如果没有找到匹配,则返回-1。
'hello'.search('ll') // 2
14.String.prototype.split()
split方法按照给定规则分割字符串,返回一个由分割出来的子字符串组成的数组。
'a|b|c'.split('|') // ["a", "b", "c"]
'a|b|c'.split('') // ["a", "|", "b", "|", "c"]
replace方法用于替换匹配的子字符串,一般情况下只替换第一个匹配(除非使用带有g修饰符的正则表达式)。
'aaa'.replace('a', 'b') // "baa"
15.String.prototype.localeCompare()
localeCompare方法用于比较两个字符串。它返回一个整数,如果小于0,表示第一个字符串小于第二个字符串;如果等于0,表示两者相等;如果大于0,表示第一个字符串大于第二个字符串。
'apple'.localeCompare('banana') // -1
'apple'.localeCompare('apple') // 0
16.repeat()
repeat方法返回一个新字符串,表示将原字符串重复n次。
'x'.repeat(3)
// 'xxx'
参数如果是小数,会被取整。
'x'.repeat(2.9)
// 'xx'
17.String.raw()
ES6 还为原生的 String 对象,提供了一个raw()方法。该方法返回一个斜杠都被转义(即斜杠前面再加一个斜杠)的字符串,往往用于模板字符串的处理方法。
String.raw`\n${2+2}`
// "\\n4"
18.includes(), startsWith(), endsWith()
传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中。ES6 又提供了三种新方法。
- includes():返回布尔值,表示是否找到了参数字符串。
- startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。
- endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部。
let s = `hello world`
s.includes(`hello`)
// true
s.startsWith(`hello`)
// true
s.endsWith(`hello`)
// false
19.padStart(),padEnd()
ES2017 引入了字符串补全长度的功能。如果某个字符串不够指定长度,会在头部或尾部补全。padStart()用于头部补全,padEnd()用于尾部补全。let b = 'x'
b.padStart(4,'ab')
// 'abax'
b.padEnd(6,'cd')
// 'xcdcdc'
20.trimStart(),trimEnd()
ES2019对字符串实例新增了trimStart()和trimEnd()这两个方法。它们的行为与trim()一致,trimStart()消除字符串头部的空格,trimEnd()消除尾部的空格。它们返回的都是新字符串,不会修改原始字符串。let t = ` hello `
t.trim() // "hello"
t.trimStart() // "hello "
t.trimEnd() // " hello"
21.matchAll()
matchAll()方法返回一个正则表达式在当前字符串的所有匹配22.replaceAll()
历史上,字符串的实例方法replace()只能替换第一个匹配。(不会修改原来的字符串)
上面例子中,replace()只将第一个e替换成了l。let g = `heelo`
g.replace('e','l') // "hlelo"
console.log(g) // "heelo"
如果要替换所有的匹配,不得不使用正则表达式的g修饰符。
正则表达式毕竟不是那么方便和直观,ES2021引入了replaceAll()方法,可以一次性替换所有匹配。(不会修改原来的字符串)g.replace(/e/g,'l') // "hlllo"
replaceAll()的第二个参数replacement除了为字符串,也可以是一个函数,该函数的返回值将替换掉第一个参数searchValue匹配的文本。g.replaceAll('e','l')
// 'hlllo'
上面例子中,replaceAll()的第二个参数是一个函数,该函数的返回值会替换掉所有b的匹配。'aabbcc'.replaceAll('b', () => '_')
// 'aa__cc'
这个替换函数可以接受多个参数。第一个参数是捕捉到的匹配内容,第二个参数捕捉到是组匹配(有多少个组匹配,就有多少个对应的参数)。此外,最后还可以添加两个参数,倒数第二个参数是捕捉到的内容在整个字符串中的位置,最后一个参数是原字符串。 ```javascript const str = ‘123abc456’; const regex = /(\d+)([a-z]+)(\d+)/g;
function replacer(match, p1, p2, p3, offset, string) { return [p1, p2, p3].join(‘ - ‘); }
str.replaceAll(regex, replacer) // 123 - abc - 456 ``` 上面例子中,正则表达式有三个组匹配,所以replacer()函数的第一个参数match是捕捉到的匹配内容(即字符串123abc456),后面三个参数p1、p2、p3则依次为三个组匹配。