Photo by Aditya Wardhana on Unsplash
20 built-in Methods for String Manipulation in JavaScript
JavaScript has several built-in methods for manipulating strings. These methods can be used to perform various tasks such as finding a string within a
Table of contents
JavaScript has several built-in methods for manipulating strings. These methods can be used to perform various tasks such as finding a string within another string, extracting characters from a string, or converting a string to uppercase or lowercase.
In this post, we will cover some of the most commonly used string methods in JavaScript which are as mentioned below:
charAt() // Returns the character at the specified index
charCodeAt() //Returns the Unicode value of the character at the specified index
concat() //Joins two or more strings and returns a new string
endsWith() //Checks whether a string ends with another string
fromCharCode() //Converts Unicode values to characters
includes() //Checks whether a string contains another string
indexOf() //Returns the position of the first found occurrence of a specified value in a string
lastIndexOf() //Returns the position of the last found occurrence of a specified value in a string
repeat() //Returns a new string with a specified number of copies of an existing string
replace() //Replaces text in a string, and returns a new string
search() //Searches for a specified value in a string, and returns the position of the match
slice() //Extracts parts of a string, and returns the extracted parts in a new string
split() //Splits a string into an array of substrings, and returns the new array
startsWith() //Checks whether a string begins with another string
substr() //Extracts parts of a string, beginning at a specified start index, and returns the new sub-string
substring() //Extracts characters from a string, between two specified indices, and returns the new sub-string
toLowerCase() //Converts a string to lowercase letters
toUpperCase() //Converts a string to uppercase letters
trim() //Removes whitespace from both sides of a string
valueOf() //Returns the primitive value of a string
Summary
In this blog post, we learned about some of the most commonly used string methods in JavaScript. These methods can be used to perform various tasks such as finding a string within another string, extracting characters from a string, or converting a string to uppercase or lowercase.
We hope you found this tutorial helpful. Happy coding!