String
Instance Methods
at
- 描述:返回索引位置字符串
- 语法:
at(index) - 参数
index = 0:索引索引边界处理if (index < 0) index = index + str.length
- 返回值:
string,undefined
'hello'.at() // 'h'
'hello'.at(0) // 'h'
'hello'.at(-1) // 'o'
'hello'.at(10) // undefinedcharAt
- 描述:返回索引位置的字符串
- 语法:
charAt(index) - 参数
index = 0:索引
- 返回值:
string,-1
'hello'.charAt() // 'h'
'hello'.charAt(1) // 'e'
'hello'.charAt(-1) // ''
'hello'.charAt(10) // ''indexOf
- 描述:返回字符串首次出现位置的索引
- 语法:
IndexOf(string, position?) - 参数
string:需要查询的字符串position = 0:查询起始位置
- 返回值:
number
'hi hi hi'.indexOf('hi') // 0
'hi hi hi'.indexOf('hi', 2) // 3
'hi hi hi'.indexOf('hello') // -1
'hi hi hi'.indexOf('') // 0lastIndexOf
- 描述:返回字符串最后一次出现位置的索引
- 语法:
lastIndexOf(string, position?) - 参数
string:需要查询的字符串position = 0:查询起始位置
- 返回值:
number
'hi hi hi'.lastIndexOf('hi') // 6
'hi hi hi'.lastIndexOf('hi', 2) // 0
'hi hi hi'.lastIndexOf('hello') // -1
'hi hi hi'.lastIndexOf('') // 8startsWith
- 描述:判断是否以某字符串开头
- 语法:
startsWith(string, position?)string:需要查询的字符串position = 0:查询起始位置
- 返回值:
boolean
'hello'.startsWith('h') // true
'hello'.startsWith('h', 1) // falseendsWith
- 描述:判断是否以某字符串结尾
- 语法:
endsWith(string, position?)string:需要查询的字符串position = str.length:查询起始位置
- 返回值:
boolean
'hello'.endsWith('o') // true
'hello'.endsWith('o', 4) // falseincludes
- 描述:判断是否包含某字符串
- 语法:
includes(string, position?)string:需要查询的字符串position = 0:查询起始位置
- 返回值:
boolean
'hello'.includes('h') // true
'hello'.includes('h', 1) // falseslice
- 描述:提取子字符串
- 语法:
slice(start, end?) - 参数
start:起点end:终点索引边界处理if (typeof start !== 'number') start = 0 if (typeof end !== 'number') end = str.length if (index < 0) index = index + str.length if (index > str.length) index = str.length if (start > end) start = end
- 返回值:
string
// 正向切片
'code-anchor'.slice() // 'code-anchor'
'code-anchor'.slice(5) // 'anchor'
'code-anchor'.slice(0, 4) // 'code'
// 逆向切片
'code-anchor'.slice(-1) // 'r'
'code-anchor'.slice(0, -1) // 'code-ancho'substring
- 描述:提取子字符串
- 语法:
slice(start, end?) - 参数
start:起点end:终点索引边界处理if (typeof start !== 'number') start = 0 if (typeof end !== 'number') end = str.length if (index < 0) index = 0 if (index > str.length) index = str.length if (start > end) [start, end] = [end, start]
- 返回值:
string
// 正向切片
'code-anchor'.substring() // 'code-anchor'
'code-anchor'.substring(5) // 'anchor'
'code-anchor'.substring(0, 4) // 'code'
// 逆向切片
'code-anchor'.substring('code-anchor'.length - 1) // 'r'
'code-anchor'.substring(0, 'code-anchor'.length - 1) // 'code-ancho'trim
- 描述:移除字符串两端空白字符 (opens in a new tab)
- 语法:
trim() - 返回值:
string
' code '.trim() // 'code'trimStart
- 描述:移除字符串左端空白字符 (opens in a new tab)
- 语法:
trimStart() - 返回值:
string
' code '.trimStart() // 'code 'trimEnd
- 描述:移除字符串右端空白字符 (opens in a new tab)
- 语法:
trimEnd() - 返回值:
string
' code '.trimEnd() // ' code'padStart
- 描述:从左填充字符串
- 语法:
PadStart(length, string?) - 参数
length:填充后,字符串的长度string = ' ':填充的字符串
- 返回值:
string
'hello'.padStart(10) // ' hello'
'hello'.padStart(10, '.') // '.....hello'
'hello'.padStart(10, '._') // '._._.hello'padEnd
- 描述:从右填充字符串
- 语法:
PadEnd(length, string?) - 参数
length:填充后,字符串的长度string = ' ':填充的字符串
- 返回值:
string
'hello'.padEnd(10) // 'hello '
'hello'.padEnd(10, '.') // 'hello.....'
'hello'.padEnd(10, '._') // 'hello._._.'toUpperCase
- 描述:将字符串转化为大写
- 语法:
toUpperCase() - 返回值:
string
'hello'.toUpperCase() // 'HELLO'toLowerCase
- 描述:将字符串转化为小写
- 语法:
toLowerCase() - 返回值:
string
'HELLO'.toLowerCase() // 'hello'replace
- 描述:替换首次匹配的值
- 语法:
replace(regexp, replacement) - 参数
regexp:正则表达式replacement:替换的字符串
- 返回值:
string
'hello'.replace(/l/, '_') // 'he_lo'replaceAll
- 描述:替换所有匹配的值
- 语法:
replaceAll(regexp, replacement) - 参数
regexp:正则表达式replacement:替换的字符串
- 返回值:
string
'hello'.replaceAll(/l/g, '_') // 'he__o'match
- 描述:返回首次匹配信息
- 语法:
match(regexp) - 参数
regexp:正则表达式
- 返回值:
array,null
'hello'.match(/o/) // ['o', index: 4, input: 'hello', groups: undefined]
'hello'.match(/x/) // nullmatchAll
- 描述:返回所有匹配信息
- 语法:
matchAll(regexp) - 参数
regexp:正则表达式
- 返回值:
iterator
'hello'.matchAll(/o/g) // iterator: [['o', index: 4, input: 'hello', groups: undefined]]
'hello'.matchAll(/x/g) // iterator: []search
- 描述:返回首次匹配的索引
- 语法:
search(regexp) - 参数
regexp:正则表达式
- 返回值:
number
'hello'.search(/o/) // 4
'hello'.search(/x/) // -1split
- 描述:分割字符串
- 语法:
split(separator, limit?) - 参数
separator:分割符limit = Infinity:数组长度上限
- 返回值:
array
'hello'.split('') // ['h', 'e', 'l', 'l', 'o']
'hi hi'.split(' ') // ['hi', 'hi']repeat
- 描述:重复复制字符串
- 语法:
repeat(count) - 参数
count = 0:重复次数
- 返回值:
string
'hi'.repeat(0) // ''
'hi'.repeat(1) // 'hi'
'hi'.repeat(2) // 'hihi'concat
- 描述:字符串拼接
- 语法:
concat(...strs) - 参数
strs:需要拼接的字符串
- 返回值:
string
'hi'.concat('-') // 'hi-'
'hi'.concat('-', 'hi') // 'hi-hi'