Name some of the string methods in javascript?


The String object lets you work with a series of characters; it wraps Javascript's string primitive data type with a number of helper methods. As JavaScript automatically converts between string primitives and String objects, you can call any of the helper methods of the String object on a string primitive.

Following are some of the methods available for strings in JavaScript −

concat() −Combines the text of two strings and returns a new string.

indexOf() −Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

lastIndexOf() −Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

match() −Used to match a regular expression against a string.

replace() −Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.

search() −Executes the search for a match between a regular expression and a specified string.

slice() −Extracts a section of a string and returns a new string.

split() −Splits a String object into an array of strings by separating the string into substrings.

substr() −Returns the characters in a string beginning at the specified location through the specified number of characters.

substring() −Returns the characters in a string between two indexes into the string.

toLowerCase() −Returns the calling string value converted to lower case.

toUpperCase() −Returns the calling string value converted to uppercase.

valueOf() −Returns the primitive value of the specified object.

Usage of some of these methods

let a = "Hello World!";
console.log(a.concat(" test"))
console.log(a.indexOf("l"))
console.log(a.lastIndexOf("l"))
console.log(a.replace("Hello", "Hi"))
console.log(a.substr(3, 7))
console.log(a.toUpperCase())

Output

Hello World! test
2
9
Hi World!
lo Worl
HELLO WORLD!

Updated on: 19-Sep-2019

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements