JS String API

Property

Property Syntax
string.length 'abc'.length // 3

Method

Method Syntax Return
String.fromCharCode() String.fromCharCode(65); "A"
- String.fromCharCode(65, 66, 67); "ABC"
String.fromCodePoint() String.fromCodePoint(65) "A"
- String.fromCodePoint(65, 66, 67); "ABC"
- String.fromCodePoint(0x2F804); "你"
charCodeAt() 'abc'.charCodeAt(0) 97
- 'abc'.charCodeAt() // the default is 0 97
- 'abc'.charCodeAt(3) // index out of range NaN
codePointAt() 'abc'.codePointAt(0) 97
- 'abc'.codePointAt() // the default is 0 97
- 'abc'.codePointAt(3) // index out of range undefined
endsWith() 'abc'.endsWith('c') true
- 'abc'.endsWith('b') false
- 'abc'.endsWith('b', 2) // check [, 2) of 'abc' true
includes() 'abc'.includes('b') true
- 'abc'.includes('b', 2) // check [2, ) of 'abc' false
indexOf() 'abca'.indexOf('a') 0
- 'abca'.indexOf('a', 1) // search in [1, ) 3
- 'abc'.indexOf('z') -1
lastIndexOf() 'abca'.lastIndexOf('a') 3
- 'abca'.lastIndexOf('a', 2) // search in [0, 2] 0
- 'abab'.lastIndexOf('ab', 2) // beginning of the match 2
match() 'ababa'.match(/a/g) ['a', 'a', 'a']
- 'abc'.match(/z/) null
padEnd() 'abc'.padEnd(4) 'abc '
- 'abc'.padEnd(6, '.') 'abc...'
padStart() 'abc'.padStart(4) ' abc'
- 'abc'.padStart(6, '.') '...abc'
repeat() 'abc'.repeat(3) 'abcabcabc'
replace() 'ababa'.replace(/a/g, 'z') 'zbzbz'
- 'ababa'.replace('a', 'z') 'zbaba'
- 'ababa'.replace(/a/g, x => x + x) 'aabaabaa'
search() 'abc'.search(/c/) 2
- 'abc'.search(/z/) -1
slice() 'abc'.slice(1) 'bc'
- 'abc'.slice(1, 2) // [1, 2) 'b'
- 'abc'.slice(-1) 'c'
- 'abc'.slice(999) ''
split() 'abc'.split('') ['a', 'b', 'c']
- 'a b c'.split(' ') ['a', 'b', 'c']
- 'a b c'.split(' ', 2) // max length of array ['a', 'b']
startsWith() 'abc'.startsWith('a') true
- 'abc'.startsWith('b') false
- 'abc'.startsWith('b', 2) // check [2, ) of 'abc' true
toLowerCase() 'Abc'.toLowerCase() 'abc'
toUpperCase() 'Abc'.toUpperCase() 'ABC'
trim() ' abc '.trim() 'abc'
trimEnd() ' abc '.trimEnd() ' abc'
trimStart() ' abc '.trimStart() 'abc '

猜你喜欢

转载自blog.csdn.net/u014510460/article/details/82824453
今日推荐