String
11 methodsImmutable sequence of UTF-16 code units.
str.split(separator?, limit?)Split the string into an array of substrings.
Parameters
| Name | Type | Description |
|---|---|---|
| separator | string | RegExp | Delimiter (string or regex). |
| limit | number | Maximum number of substrings. |
Returns
string[]
Example
'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)Return a new string with the first match of pattern replaced. Use replaceAll for all matches.
Parameters
| Name | Type | Description |
|---|---|---|
| pattern | string | RegExp | Pattern to match. |
| replacement | string | Function | Replacement string or function. |
Returns
string
Example
'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)Return the result of matching a string against a regular expression.
Parameters
| Name | Type | Description |
|---|---|---|
| regexp | RegExp | Regular expression (string is coerced). |
Returns
Array | null
Example
'2024-01-15'.match(/(\d{4})-(\d{2})-(\d{2})/)
// ['2024-01-15', '2024', '01', '15']
'abc'.match(/\d/) // nullstr.trim()Return a new string with whitespace removed from both ends.
Returns
string
Example
' hi '.trim() // 'hi'
'\n hello\n'.trim() // 'hello'str.padStart(targetLength, padString?)Pad the string from the start with padString until it reaches targetLength.
Parameters
| Name | Type | Description |
|---|---|---|
| targetLength | number | Final length. |
| padString | string | Padding string (default space). |
Returns
string
Example
'5'.padStart(3, '0') // '005'
'abc'.padStart(6, '*') // '***abc'str.padEnd(targetLength, padString?)Pad the string from the end with padString until it reaches targetLength.
Parameters
| Name | Type | Description |
|---|---|---|
| targetLength | number | Final length. |
| padString | string | Padding string (default space). |
Returns
string
Example
'5'.padEnd(3, '0') // '500'
'abc'.padEnd(6, '*') // 'abc***'str.includes(searchString, position?)Return true if searchString is found within the string.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | Substring to search for. |
| position | number | Start position (default 0). |
Returns
boolean
Example
'hello world'.includes('world') // true
'hello'.includes('a') // falsestr.startsWith(searchString, position?)Return true if the string starts with searchString.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | Prefix to check. |
| position | number | Start position (default 0). |
Returns
boolean
Example
'hello'.startsWith('he') // true
'hello'.startsWith('el', 1) // truestr.endsWith(searchString, length?)Return true if the string ends with searchString.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | Suffix to check. |
| length | number | Consider the string as length characters long. |
Returns
boolean
Example
'hello'.endsWith('lo') // true
'hello'.endsWith('ell', 4) // truestr.repeat(count)Return a new string consisting of count copies of the original.
Parameters
| Name | Type | Description |
|---|---|---|
| count | number | Number of copies. |
Returns
string
Example
'ab'.repeat(3) // 'ababab'
'-'.repeat(5) // '-----'str.substring(start, end?)Return the substring from start to end (end excluded).
Parameters
| Name | Type | Description |
|---|---|---|
| start | number | Start index. |
| end | number | End index (default length). |
Returns
string
Example
'hello'.substring(1, 3) // 'el'
'hello'.substring(2) // 'llo'