字符串方法
codePointAt
ES6 方法
str.codePointAt(pos)
- 参数 : pos 字符串中需要转码的位置
- 返回值 : 返回字符串给定位置的编码单元
codePointAt() 方法返回 一个 Unicode 编码点值的非负整数。
charCodeAt
harCodeAt() 方法返回 0 到 65535 之间的整数,表示给定索引处的 UTF-16 代码单元 如果 Unicode 码点不能用一个 UTF-16 编码单元表示(因为它的值大于 0xFFFF) js 采用的是 UTF-16 存储,两个字节存储。对于超过 ffff 后就需要 4 个字节存储,十六进制就成了 5 位。对于 4 个字节的字符,js 不能争取处理了
console.log('𠮷'.charCodeAt(0)) // 55362
console.log('𠮷'.codePointAt(0)) // 134071
console.log('𠮷'.charCodeAt(0).toString(16)) // 20bb7
console.log('𠮷'.codePointAt(0).toString(16)) // 20bb7
concat
字符串拼接,性能差。强烈建议使用赋值操作符(+, +=)代替 concat 方法。
let hello = 'Hello, '
console.log(hello.concat('Kevin', '. Have a nice day.'))
// Hello, Kevin. Have a nice day.
let greetList = ['Hello', ' ', 'Venkat', '!']
''.concat(...greetList) // "Hello Venkat!"
''.concat({}) // [object Object]
''.concat([]) // ""
''.concat(null) // "null"
''.concat(true) // "true"
''.concat(4, 5) // "45"
String.fromCharCode
静态方法 返回指定的 UTF-16 代码单元创建的字符串
String.fromCodePoint
返回使用指定的代码点序列创建的字符串。
console.log(String.fromCharCode(189, 43, 190, 61)) // "½+¾="
console.log(String.fromCodePoint(9731, 9733, 9842, 0x2f804)) //"☃★♲你"
String.prototype.includes()
ES6
str.includes(searchString[, position]) position 从哪个位置开始查找
String.prototype.indexOf()
str.indexOf(searchValue [, fromIndex])
//奇诡的空字符
'hello world'.indexOf('') // 返回 0
'hello world'.indexOf('', 0) // 返回 0
'hello world'.indexOf('', 3) // 返回 3
'hello world'.indexOf('', 8) // 返回 8
//超过长度,返回长度
'hello world'.indexOf('', 11) // 返回 11
'hello world'.indexOf('', 13) // 返回 11
'hello world'.indexOf('', 22) // 返回 11
//区分大小写,不存在返回-1
'Blue Whale'.indexOf('Blue') // 返回 0
'Blue Whale'.indexOf('Blute') // 返回 -1
'Blue Whale'.indexOf('Whale', 0) // 返回 5
'Blue Whale'.indexOf('Whale', 5) // 返回 5
'Blue Whale'.indexOf('', -1) // 返回 0
'Blue Whale'.indexOf('', 9) // 返回 9
'Blue Whale'.indexOf('', 10) // 返回 10
'Blue Whale'.indexOf('', 11) // 返回 10
String.prototype.localeCompare()
'a'.localeCompare('c') //-1
'a'.localeCompare('a') //0
'check'.localeCompare('against') //1
String.prototype.match()
str.match(regexp)
regexp 正则表达式对象
return
1.如果使用 g 标志,则将返回与完整正则表达式匹配的所有结果,但不会返回捕获组。
2.如果未使用 g 标志,则仅返回第一个完整匹配及其相关的捕获组(Array)。 在这种情况下,返回的项目将具有如下所述的其他属性。
- groups: 一个捕获组数组 或 undefined(如果没有定义命名捕获组)。
- index: 匹配的结果的开始位置
- input: 搜索的字符串.
var str = 'For more information, see Chapter 3.4.5.1'
var re = /see (chapter \d+(\.\d)*)/i
var found = str.match(re)
console.log(found)
// logs=> [
// 0: 'see Chapter 3.4.5.1',
// 1: 'Chapter 3.4.5.1',
// 2: '.1',
// groups: undefined
// index: 22,
// input: 'For more information, see Chapter 3.4.5.1'
// ]
// 'see Chapter 3.4.5.1' 是整个匹配。
// 'Chapter 3.4.5.1' 被'(chapter \d+(\.\d)*)'捕获。
// '.1' 是被'(\.\d)'捕获的最后一个值。
// groups: undefined
// 'index' 属性(22) 是整个匹配从零开始的索引。
// 'input' 属性是被解析的原始字符串。
//忽略大小写
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
var regexp = /[A-E]/gi
var matches_array = str.match(regexp)
console.log(matches_array)
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
一个非正则表达式对象作为参数
当参数是一个字符串或一个数字,它会使用 new RegExp(obj)来隐式转换成一个 RegExp。如果它是一个有正号的正数,RegExp() 方法将忽略正号。
var str1 = 'NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.',
str2 = 'My grandfather is 65 years old and My grandmother is 63 years old.',
str3 = 'The contract was declared null and void.'
str1.match('number') // "number" 是字符串。返回["number"]
str1.match(NaN) // NaN的类型是number。返回["NaN"]
str1.match(Infinity) // Infinity的类型是number。返回["Infinity"]
str1.match(+Infinity) // 返回["Infinity"]
str1.match(-Infinity) // 返回["-Infinity"]
str2.match(65) // 返回["65"]
str2.match(+65) // 有正号的number。返回["65"]
str3.match(null) // 返回["null"]
String.prototype.matchAll()
str.matchAll(regexp)
- regexp :正则,必须是设置了全局模式 g 的形式
- return :一个迭代器(不可重用,结果耗尽需要再次调用方法,获取一个新的迭代器)。
- 返回的迭代器可以用 [...return] 得到所有结果
与 match 相比有 3 个区别:
- 它返回包含匹配项的可迭代对象,而不是数组。我们可以用 Array.from 从中得到一个常规数组。
- 每个匹配项均以包含分组的数组形式返回(返回格式与不带 g 标记的 str.match 相同)。
- 如果没有结果,则返回的不是 null,而是一个空的可迭代对象。
转数组的方法
// 转为数组的方法一
;[...string.matchAll(regex)]
// 转为数组的方法二
Array.from(string.matchAll(regex))
const regexp = RegExp('foo[a-z]*', 'g')
const str = 'table football, foosball'
const matches = str.matchAll(regexp)
for (const match of matches) {
console.log(`Found ${match[0]} start=${match.index} end=${match.index + match[0].length}.`)
}
// expected output: "Found football start=6 end=14."
// expected output: "Found foosball start=16 end=24."
// matches iterator is exhausted after the for..of iteration
// Call matchAll again to create a new iterator
Array.from(str.matchAll(regexp), (m) => m[0])
// Array [ "football", "foosball" ]
;``
matchAll 更好的获取捕获组 match() 和/g 标志时 捕获组会被忽略
为什么这个方法这样设计?原因很简单 — 为了优化。 调用 matchAll 不会执行搜索。相反,它返回一个可迭代的对象,最初没有结果。每当我们对它进行迭代时才会执行搜索,例如在循环中。 因此,这将根据需要找到尽可能多的结果,而不是全部。 例如,文本中可能有 100 个匹配项,但是在一个 for..of 循环中,我们已经找到了 5 个匹配项,然后觉得足够了并做出一个 break。这时引擎就不会花时间查找其他 95 个匹配。
var regexp = /t(e)(st(\d?))/g
var str = 'test1test2'
str.match(regexp)
// Array ['test1', 'test2']
let array = [...str.matchAll(regexp)]
array[0]
// ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', length: 4]
array[1]
// ['test2', 'e', 'st2', '2', index: 5, input: 'test1test2', length: 4]
String.prototype.normalize()
normalize() 方法会按照指定的一种 Unicode 正规形式将当前字符串正规化。
没看,用到再看吧
String.prototype.padEnd()
ES2017
str.padEnd(targetLength [, padString])
- targetLength 当前字符串填充后要达到的长度
- padString 填充字符串。如果字符串太长,只保留部分
'abc'.padEnd(10) // "abc "
'abc'.padEnd(10, 'foo') // "abcfoofoof"
'abc'.padEnd(6, '123456') // "abc123"
'abc'.padEnd(1) // "abc"
String.prototype.padStart()
str.padStart(targetLength [, padString])
- targetLength 当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身。
- padString 填充字符串。如果字符串太长,使填充后的字符串长度超过了目标长度,则只保留最左侧的部分
'abc'.padStart(10) // " abc"
'abc'.padStart(10, 'foo') // "foofoofabc"
'abc'.padStart(6, '123465') // "123abc"
'abc'.padStart(8, '0') // "00000abc"
'abc'.padStart(1) // "abc"
String.prototype.repeat()
str.repeat(count) 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。
String.prototype.replace()
str.replace(regexp|substr, newSubStr|function)
- 1.使用正则 使用了正则表达式及忽略大小写标示。
var str = 'Twas the night before Xmas...'
var newstr = str.replace(/xmas/i, 'Christmas')
console.log(newstr) // Twas the night before Christmas...
- 2.使用 global 和 ignore 选项
var re = /apples/gi
var str = 'Apples are round, and apples are juicy.'
var newstr = str.replace(re, 'oranges')
console.log(newstr) //// oranges are round, and oranges are juicy.
- 交换位置 使用$1 和 $2 代替替换文本。
var re = /(\w+)\s(\w+)/
var str = 'John Smith'
var newstr = str.replace(re, '$2, $1')
// Smith, John
console.log(newstr)
- 华氏温度转摄氏
function f2c(x) {
function convert(str, p1, offset, s) {
return ((p1 - 32) * 5) / 9 + 'C'
}
var s = String(x)
var test = /(\d+(?:\.\d*)?)F\b/g
return s.replace(test, convert)
}
String.prototype.replaceAll()
const newStr = str.replaceAll(regexp|substr, newSubstr|function) regex 必现有全局 g 标志
String.prototype.search()
str.search(regexp)
返回索引
String.prototype.slice()
str.slice(beginIndex[, endIndex])
- beginIndex 起始位置,如果值为负数,会被当做 strLength + beginIndex 看待.
- endIndex 结束位置,但不含,如果小于 0. 则被看作是 strLength + endIndex.
var str = 'The morning is upon us.'
str.slice(-3) // 返回 'us.'
str.slice(-3, -1) // 返回 'us'
str.slice(0, -1) // 返回 'The morning is upon us'
String.prototype.split()
分割字串
String.prototype.startsWith()
str.startsWith(searchString[, position])
判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回
String.prototype.substring()
str.substring(indexStart[, indexEnd])
- indexStart : 截取的起始位置
- indexEnd : 一个 0 到字符串长度之间的整数,以该数字为索引的字符不包含在截取的字符串内
参数说明
- 如果 indexStart 等于 indexEnd,substring 返回一个空字符串。
- 如果省略 indexEnd,substring 提取字符一直到字符串末尾。
- 如果任一参数小于 0 或为 NaN,则被当作 0。
- 如果任一参数大于 stringName.length,则被当作 stringName.length。
- 如果 indexStart 大于 indexEnd,则 substring 的执行效果就像两个参数调换了一样。见下面的例子。
var anyString = 'Mozilla'
// 输出 "Moz"
console.log(anyString.substring(0, 3))
console.log(anyString.substring(3, 0))
console.log(anyString.substring(3, -3))
console.log(anyString.substring(3, NaN))
console.log(anyString.substring(-2, 3))
console.log(anyString.substring(NaN, 3))
// 输出 "lla"
console.log(anyString.substring(4, 7))
console.log(anyString.substring(7, 4))
// 输出 ""
console.log(anyString.substring(4, 4))
// 输出 "Mozill"
console.log(anyString.substring(0, 6))
// 输出 "Mozilla"
console.log(anyString.substring(0, 7))
console.log(anyString.substring(0, 10))
String.prototype.toLocaleLowerCase()
locale 可选
toLocaleLowerCase 小写 原值不变
String.prototype.toLowerCase()
小写 原值不变
String.prototype.toLocaleUpperCase()
toLocaleUpperCase 大写 原值不变
String.prototype.toUpperCase()
大写 原值不变
const a = String.prototype.toUpperCase.call({
toString: function toString() {
return 'abcdef'
},
})
const b = String.prototype.toUpperCase.call(true)
// 输出 'ABCDEF TRUE'。
console.log(a, b)
String.prototype.toString()
String 对象覆盖了 Object 对象的 toString 方法;并没有继承 Object.toString()。对于 String 对象,toString 方法返回该对象的字符串形式,和 String.prototype.valueOf() 方法返回值一样。
String.prototype.trim()
删除两端空白字符
String.prototype.trimRight()
trimEnd() 方法从一个字符串的末端移除空白字符。trimRight() 是这个方法的别名。
String.prototype.trimStart()
trimStart() 方法从字符串的开头删除空格。trimLeft() 是此方法的别名。
String.prototype.valueOf()
返回 String 对象的原始值
const stringObj = new String('foo')
console.log(stringObj)
// expected output: String { "foo" }
console.log(stringObj.valueOf())
// expected output: "foo"