String
11 methodsUTF-16 码单元的不可变序列。
str.split(separator?, limit?)将字符串分割为子字符串数组。
Parameters
| Name | Type | Description |
|---|---|---|
| separator | string | RegExp | 分隔符(字符串或正则表达式)。 |
| limit | number | 子字符串的最大数量。 |
Returns
string[]
Example
javascript
'a,b,c'.split(',') // ['a', 'b', 'c']
'hello'.split('') // ['h','e','l','l','o']
'a-b-c'.split('-', 2) // ['a', 'b']str.replace(pattern, replacement)返回一个新字符串,替换 pattern 的第一个匹配。要替换所有匹配请使用 replaceAll。
Parameters
| Name | Type | Description |
|---|---|---|
| pattern | string | RegExp | 要匹配的模式。 |
| replacement | string | Function | 替换字符串或函数。 |
Returns
string
Example
javascript
'hello world'.replace('world', 'JS') // 'hello JS'
'a-b-c'.replace('-', '_') // 'a_b-c'
'a-b-c'.replace(/-/g, '_') // 'a_b_c'str.match(regexp)返回字符串与正则表达式匹配的结果。
Parameters
| Name | Type | Description |
|---|---|---|
| regexp | RegExp | 正则表达式(字符串会被强制转换)。 |
Returns
Array | null
Example
javascript
'2024-01-15'.match(/(\d{4})-(\d{2})-(\d{2})/)
// ['2024-01-15', '2024', '01', '15']
'abc'.match(/\d/) // nullstr.trim()返回一个新字符串,移除两端的空白字符。
Returns
string
Example
javascript
' hi '.trim() // 'hi'
'\n hello\n'.trim() // 'hello'str.padStart(targetLength, padString?)从起始位置用 padString 填充字符串,直到达到 targetLength。
Parameters
| Name | Type | Description |
|---|---|---|
| targetLength | number | 最终长度。 |
| padString | string | 填充字符串(默认为空格)。 |
Returns
string
Example
javascript
'5'.padStart(3, '0') // '005'
'abc'.padStart(6, '*') // '***abc'str.padEnd(targetLength, padString?)从末尾用 padString 填充字符串,直到达到 targetLength。
Parameters
| Name | Type | Description |
|---|---|---|
| targetLength | number | 最终长度。 |
| padString | string | 填充字符串(默认为空格)。 |
Returns
string
Example
javascript
'5'.padEnd(3, '0') // '500'
'abc'.padEnd(6, '*') // 'abc***'str.includes(searchString, position?)如果在字符串中找到 searchString 则返回 true。
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | 要搜索的子字符串。 |
| position | number | 起始位置(默认 0)。 |
Returns
boolean
Example
javascript
'hello world'.includes('world') // true
'hello'.includes('a') // falsestr.startsWith(searchString, position?)如果字符串以 searchString 开头则返回 true。
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | 要检查的前缀。 |
| position | number | 起始位置(默认 0)。 |
Returns
boolean
Example
javascript
'hello'.startsWith('he') // true
'hello'.startsWith('el', 1) // truestr.endsWith(searchString, length?)如果字符串以 searchString 结尾则返回 true。
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | 要检查的后缀。 |
| length | number | 将字符串视为长度为 length 个字符。 |
Returns
boolean
Example
javascript
'hello'.endsWith('lo') // true
'hello'.endsWith('ell', 4) // truestr.repeat(count)返回由原字符串重复 count 次组成的新字符串。
Parameters
| Name | Type | Description |
|---|---|---|
| count | number | 重复次数。 |
Returns
string
Example
javascript
'ab'.repeat(3) // 'ababab'
'-'.repeat(5) // '-----'str.substring(start, end?)返回从 start 到 end(不包含 end)的子字符串。
Parameters
| Name | Type | Description |
|---|---|---|
| start | number | 起始索引。 |
| end | number | 结束索引(默认为 length)。 |
Returns
string
Example
javascript
'hello'.substring(1, 3) // 'el'
'hello'.substring(2) // 'llo'