JavaScript String Methods
In this blog, we will learn about mostly used JavaScript String Methods
charAt()
TheString.charAt()
method returns the character at the specified index (position)
charCodeAt()
TheString.charCodeAt()
method returns unicode of the character at the specified index in a string.
codePointAt()
TheString.codePointAt()
method returns a non-negative integer value i.e, code point value of the specified element of the given string.
concat()
TheString.concat()
method joins two or more strings, and returns a new joined strings
endsWith()
TheString.endsWith()
method checks whether a string ends with specified string/characters
fromCharCode()
TheString.fromCharCode()
method converts Unicode values to characters
fromCodePoint()
includes()
TheString.includes()
method checks whether a string contains the specified string/characters
indexOf()
TheString.indexOf()
method returns the position of the first found occurrence of a specified value in a string
lastIndexOf()
TheString.lastIndexOf()
method returns the position of the last found occurrence of a specified value in a string
localeCompare()
TheString.localCompare()
method compares two strings in the current locale.
It returns a number, indicating whether the reference string comes before, after or is the same as the compareString in sort order.
Returns one of three values:
- -1 if the reference string is sorted before the compareString
- 0 if the two strings are equal
- 1 if the reference string is sorted after the compareString
match()
TheString.match()
method searches a string for a match against a regular expression, and returns the matches
repeat()
TheString.repeat()
method returns a new string with a specified number of copies of an existing string
replace()
TheString.replace()
method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced
search()
TheString.search()
method searches a string for a specified value, or regular expression, and returns the position of the match
Return value will be the index of the first match between the regular expression and the given string, or -1
if no match was found.
slice()
TheString.slice()
method extracts a part of a string and returns a new string
split()
TheString.split()
method splits a string into an array of substrings
String.prototype.startsWith()
TheString.startsWith()
method Checks whether a string begins with specified characters
substring()
TheString.substring()
method extracts the characters from a string, between two specified indices
toLocaleLowerCase()
TheString.toLocaleLowerCase()
method converts a string to lowercase letters, according to the host’s locale
toLocaleUpperCase()
TheString.toLocaleUpperCase()
method converts a string to uppercase letters, according to the host’s locale
toLowerCase()
TheString.toLowerCase()
method converts a string to lowercase letters
toSource()
TheString.toSource()
method returns the value of a String object
toString()
TheString.toString()
method converts a string to uppercase letters
trim()
TheString.trim()
method removes whitespace from both ends of a string
valueOf()
TheString.valueOf()
method returns the primitive value of a String object