Here's a cheatsheet of the latest ECMAScript string methods in Markdown format:
length
string.length
Returns the number of characters in a string.
'Hello World'.length
Results: 11
toUpperCase()
String.prototype.toUpperCase()
Converts all characters in a string to uppercase.
'hello'.toUpperCase()
Results: 'HELLO'
toLowerCase()
String.prototype.toLowerCase()
Converts all characters in a string to lowercase.
'HELLO'.toLowerCase()
Results: 'hello'
indexOf()
String.prototype.indexOf(searchValue: string, fromIndex?: number)
Returns the index of the first occurrence of a specified value in a string.
'Hello World'.indexOf('World')
Results: 6
slice()
String.prototype.slice(start?: number, end?: number)
Extracts a section of a string and returns it as a new string.
'Hello World'.slice(0, 5)
Results: 'Hello'
replace()
String.prototype.replace(searchValue: string | RegExp, replacement: string | (substring: string, ...args: any[]) => string)
Replaces a specified value or pattern in a string with another value.
'Hello World'.replace('World', 'there')
Results: 'Hello there'
split()
String.prototype.split(separator?: string | RegExp, limit?: number)
Splits a string into an array of substrings based on a specified separator.
'Hello World'.split(' ')
Results: ['Hello', 'World']
trim()
String.prototype.trim()
Removes whitespace from both ends of a string.
' Hello World '.trim()
Results: 'Hello World'
charAt()
String.prototype.charAt(index: number)
Returns the character at a specified index in a string.
'Hello'.charAt(0)
Results: 'H'
concat()
String.prototype.concat(...strings: string[])
Concatenates the string arguments to the calling string and returns a new string.
'Hello'.concat(' ', 'World')
Results: 'Hello World'
Sorting based on the most frequently used string methods, ordered accordingly
Top comments (0)