String Method’s In Javascript🚀
Table of contents
In Javascript, many string methods are either built-in or user-defined. Built-in string methods are the methods that are present in any programming language library.
Built-in string methods of JavaScript:
search() : — It is used to search a string for a specific value or an expression. It returns the position of the match.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.search("jha"); console.log(a); } myfunction(); Output:- 12
split(): — It is used to split a string into an array of substrings.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.split(" "); console.log(a); } myfunction(); Output:- ["Pawan", "kumar", "jha"] 0:"Pawan" 1:"kumar" 2:"jha"
startsWith(): — It is used to check whether a string begins with the specified characters.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.startsWith("Pawan"); console.log(a); } myfunction(); Output:- true
slice(): — It is used to extract a part of a string and return a new string.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.slice(0,6); console.log(a); } myfunction(); Output:- Pawan
concat(): — It is used to combine texts of two strings and return a new string.
Code:- function myfunction(){ var str = "Pawan kumar "; var str1 = "Jha" var a = str.concat(str1); console.log(a); } myfunction(); Output:- Pawan kumar Jha
charAt(): — It is used to return the character at the specified index.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.charAt(6); console.log(a); } myfunction(); Output:- k
indexOf(): — It is used to return the index, within the string object, which occurs first, of the specified value. It returns -1 if the object is not found.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.indexOf("Pawan"); console.log(a); } myfunction(); Output:- 0
lastIndexOf(): — It is used to return the index, within the string object, which occurs last, of the specified value. It returns -1 if the object is not found.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.indexOf("Jha"); console.log(a); } myfunction(); Output:- 12
match(): — It is used to match a regular expression against a string.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.match("Pawan"); console.log(a); } myfunction(); Output:- ["Pawan", 0, "Pawan kumar Jha", unde...] 0:"Pawan"
replace(): — It is used to find a match between a regular expression and a string. The matched substring is replaced with a new substring.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.replace("kumar", ""); console.log(a); } myfunction(); Output:- Pawan Jha
substr(): — It is used to return the characters in a string, beginning at the specified location, through the specified number of characters.
Code:- Approach - 1: function myfunction(){ var str = "Pawan kumar jha"; var a = str.substr(5,6); console.log(a); } myfunction(); Output:- kumar Approach - 2: function myfunction(){ var str = "Pawan kumar jha"; var a = str.substr(5); console.log(a); } myfunction(); Output:- kumar Jha
substring(): — It is used to return the characters in a string between the two specified indexes.
Code:- Approach - 1: function myfunction(){ var str = "Pawan kumar jha"; var a = str.substring(6,12); console.log(a); } myfunction(); Output:- kumar Approach - 2: Code:- Approach - 2: function myfunction(){ var a = str.substring(6,12); console.log(a); } myfunction(); Output:- kumar
toLowerCase(): — It is used to convert the string value which is called, to lower case.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.toLowerCase(); console.log(a); } myfunction(); Output:- pawan kumar jha
toUpperCase(): — It is used to convert the string value which is called, to upper case.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.toUpperCase(); console.log(a); } myfunction(); Output:- PAWAN KUMAR JHA
valueOf(): — It is used to return the primitive value of the specified object.
Code:- function myfunction(){ var str = "Pawan kumar jha"; var a = str.valueOf(); console.log(a); } myfunction(); Output:- Pawan kumar Jha
🚀Thank You🚀