String.prototype.charAt(index: number): string
: Returns the character(exactly one UTF-16 code unit) at the specified index.1
'hello world'.charAt(1) // returns 'e'
String.prototype.charCodeAt(index: number): number
: Returns a number that is the UTF-16 code unit value at the specified index.1
'hello world'.charCodeAt(1) // returns 101
String.prototype.codePointAt(index: string): number
: Returns a nonnegative integer Number that is the code point value of the UTF-16 encoded code point starting at the specified index.1
'hello world'.codePointAt(1) // returns 101
String.prototype.concat(str: string, ...rest: string[]): string
: Combines the texts and returns a new string.1
'hello world'.concat(', ', 'and welcome') // returns 'hello world, and welcome'
String.prototype.includes(str: string): boolean
: Determines whether one string may be found within another string.1
'hello world'.includes('world') // returns true
String.prototype.endsWith(str: string): boolean
: Determines whether a string ends with the characters of another string.1
'hello world'.endsWith('world') // returns true
String.prototype.indexOf(str: string): number
: Returns the index of the specified string, -1 for not found.1
s.indexOf('world') // returns 6
String.prototype.lastIndexOf(str: string)
: Returns the index of the specified string, but search starts from the end, -1 for not found1
2'world hello world'.indexOf('world') // returns 0
'world hello world'.lastIndexOf('world') // returns 12String.prototype.localeCompare(str: string)
: Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16const a = 'réservé' // with accents, lowercase
const b = 'RESERVE' // no accents, uppercase
console.log(a.localeCompare(b))
// expected output: 1
console.log(a.localeCompare(b, 'en', { sensitivity: 'base' }))
// expected output: 0
// The letter "a" is before "c" yielding a negative value
'a'.localeCompare('c') // -2 or -1 (or some other negative value)
// Alphabetically the word "check" comes after "against" yielding a positive value
'check'.localeCompare('against') // 2 or 1 (or some other positive value)
// "a" and "a" are equivalent yielding a neutral value of zero
'a'.localeCompare('a') // 0String.prototype.match(regexp): string[] | null
: Used to match a regular expression against a string, null for not found1
2
3const paragraph = `The quick brown fox jumps over the lazy dog. It barked`
const regex = /[A-Z]/g
paragraph.match(regex) // returns ["T", "I"]String.prototype.matchAll(regex): Iterator
: Returns an iterator of all matches.String.prototype.normalize
: Returns the Unicode Normalization Form of the calling string value.String.prototype.padEnd()
: Pads the current string with a given string(repeated if needed) so that the resulting string reaches a given length. The padding is appliced fromthe end of the current string.1
'Breaded Mushrooms'.padEnd(25, '.') // returns "Breaded Mushrooms........"
String.prototype.padStart()
: Pads the current string from the start with a given string to create a new string from a given length1
'Breaded Mushrooms'.padStart(25, '.') // returns "........Breaded Mushrooms"
String.prototype.repeat(times: number)
: Returns a string consisting of the elements of the object repeated the given times.1
'hello world'.repeat(3) // "hello worldhello worldhello world"
String.prototype.replace(pattern: regexp | string, replacement): string
: Returns a new string with some or all matches of apattern
replaced by areplacement
1
'hello world'.replace(/world/, 'hello') // returns 'hello hello'
String.prototype.search(pattern: regexp): number
: Executes a search for a match between a regular expression and this string, returns the index of matched string.1
'hello world'.search(/ll/) // returns 2
String.prototype.slice(start: number[, end: number]): string
: Returns a new string extracted from the original string from [start, end).1
'hello'.slice(1, 2) // returns 'e'
String.prototype.split(pattern): string[]
: Splits a string into an array of strings by separating the string into substrings.1
'hello world'.split(' ') // ['hello', 'world']
String.prototype.startsWith()
: Determines whether a string begins with the characters of another string.String.prototype.substring
: Returns the characters in a string between two indices into the string.1
'hello world'.substring(1, 2) // returns 'e'
It seems that the
String.prototype.substring
works likeString.prototype.slice
, but if thestart > end
,String.prototype.substring
swaps the two parameters whileString.prototype.slice
return ‘’.1
2'hello world'.substring(2, 1) // returns 'e'
'hello world'.slice(2, 1) // returns ''And
String.prototype.slice
accepts netagive indices as start from the end of the string, whileString.prototype.substring
treats negative and NaN as 0.Note
String.prototype.substr
is deprecatedString.prototype.toLocaleLowerCase()
: The characters within a string are converted to lower case while respecting the current locale. For most languages, this will return the same astoLowerCase()
String.prototype.toLocaleUpperCase()
: The characters within a string are converted to upper case while respecting the current locale. For most languages, this will return the same astoUpperCase()
String.prototype.toLowerCase()
: Returns the calling string value converted to lower case.1
'HELLO WORLD'.toLowerCase() // returns 'hello world'
String.prototype.toUpperCase()
: Returns the calling string value converted to upper case.1
'hello world'.toUpperCase() // returns 'HELLO WORLD'
String.prototype.toString()
: Returns a string representing the specified object.String.prototype.trim()
: Trims whitespaces from the beginning and end of a string1
' hello world '.trim() // returns 'hello wrold'
String.prototype.trimStart()
,String.prototype.trimLeft()
: Trims whitespaces from the beginning of the string.1
' hello world '.trimStart() // returns 'hello world '
String.prototype.trimEnd()
,String.prototype.trimRight()
: Trims whitespace from the end of the string.1
' hello world '.trimEnd() // return ' hello world'
String.prototype.valueOf
: Returns the primitive value of the specified object.
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment