JavaScript provides lots of string methods to perform operations on string variables. Here, we will learn all the string methods with examples.
What is the String?
The string is a collection of one or more characters written inside quotes.
Example
var m = "Erroser is the best website for learning";
Note: We can use single quotes, too.
Example
var n = 'Errosea is love';
JavaScript provides different string methods that help users to work with strings.
Index
This property returns the length of a string.
Example
var x = "Errorsea";
var len = x.length; // result: 8
JavaScript provides indexOf() method that returns the index of (the position of) the first occurrence of a specified text in a string:
Example
var x = "have you ever visit Errorsea";
var position = x.indexOf("Errorsea"); // result: 20
Note: JavaScript counts positions from zero. It returns -1 if the string is not found in the whole string.
JavaScript provides the lastIndexOf() method that returns the index of (the position of) the last occurrence of a specified text in a string:
Example
var x = "Do visit Errosea because Errosea is the best learning platform";
var position = x.lastIndexOf("Errorsea"); // result: 25
Note: JavaScript counts positions from zero. It returns -1 if the string is not found in the whole string.
This method extracts a particular part of a string and returns the extracted part as a new string.
The method takes two parameters: the start position and the end position (end not included).
Syntax
slice(start, end)
Here,
start: start position
end: end position
Example 1
var x = "HTML, CSS, JAVASCRIPT";
var result = x.slice(6,9); // result: "CSS"
If we don’t write the end position, then JS will take the end of a string as an end position.
Example 2
var x = "HTML, CSS, JAVASCRIPT";
var result = x.slice(6); // result: "CSS, JAVASCRIPT"
Note: It will take the end of the string as an end position. If a parameter is negative, the position is counted from the end of the string.
Example 3
var x = "HTML, CSS, JAVASCRIPT";
var result = x.slice(-15,-12); // result: "CSS"
Note: It will count positions from the end of the string.
This method extracts a particular part of a string and returns the extracted part as a new string.
Note: The difference is that substr() cannot accept negative indexes.
Syntax
substr(start, end)
Here,
start: start position
end: end position
Example 1
var x = "HTML, CSS, JAVASCRIPT";
var result = x.substr(6,9); // result: "CSS"
If we don’t write the end position, then JS will take the string’s end as an end position.
Example 2
var x = "HTML, CSS, JAVASCRIPT";
var result = x.substr(6); // result: "CSS, JAVASCRIPT"
Note: It will take the end of the string as an end position.
The replace() function replaces a specified value with another value in a string.
Example
<!DOCTYPE html>
<html>
<body>
<h2>Errorsea - JavaScript String Methods</h2>
<p>Replace "facebook" with "Errorsea" in the paragraph below:</p>
<button >
Explanation: The above code replaces facebook with Errorsea and places it inside the <p> tag.
Note: It replaces only the first match string. This method is case sensitive.
We can convert the string into uppercase using the toUpperCase() method.
Example
var x = "Errorsea";
var upr = x.toUpperCase(); // result: "ERRORSEA"
We can convert the string into uppercase using the toLowerCase() method.
Example
var x = "Errorsea";
var lwr = x.toLowerCase(); // result: "errorsea"
This method is used to join two or more strings.
Example 1
var m = "Error";
var n = "sea";
var x = m.concat(n); // result: "Errorsea"
Note: This method can be used instead of the plus operator.
Example 2
var x = "Error" + "sea" ;
This method removes whitespace from both sides of a string.
Example
var x = " Errorsea ";
var trm = x.trim(); // result: "Errorsea"
Note: The trim() method is not supported in Internet Explorer 8 or lower.
This method returns the character at a specified index (position) in a string.
Example
var x = "Errorsea";
var ch = x.charAt(0); // returns E
This method returns the Unicode of the character at a specified index in a string.
Example
var x = "Errorsea";
var code = x.charCodeAt(0); //result : 69
In JS, a string can be converted to an array with the split() method. This method split the string and store it in the array.
Example
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the first array element, after a string split.</p>
<button >
Explanation: Above code split string contains variable x using the split() function and store it in the array. We show first element of array in innerHTML of <p> tag.
This method determines whether a string ends with a specified string’s characters.
Example
var x = "Errorsea is love"
var result = x.endsWith("love"); // result: true
var result2 = x.endsWith("hello"); // result: false
Note: It returns true if the string matches with the string at the end. It returns false if the string doesn’t match with the string at the end.
This method converts Unicode values into characters.
Example
var x = String.fromCharCode(69); // result: "E"
This method determines whether a string contains the characters of a specified string.
Example
var x = "Have you visit Errorsea";
var result = x.includes("Errorsea"); //result: true
Note: It returns true if the string contains a specific string’s characters. It returns false if the string doesn’t contain a specific string’s characters.
This method is used for comparing two strings in the current locale.
Example
var str1 = "ab";
var str2 = "cd";
var x = str1.localCompare(str2); // result : -1
Note:
This method returns a new string with a specified number of copies of the string it was called on.
Example
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the extracted part of the string.</p>
<button >
Javascript provides the search() method that returns the index of a specified text in a string:
Example
var x = "have you ever visit Errorsea";
var position = x.search("Errorsea"); // result: 20
This method is used to determine whether a string starts with a specified string’s characters.
Example
var x = "Errorsea is love"
var result = x.startssWith("Errorsea"); // result: false
var result2 = x.startssWith("hello"); // result: true
Note: It returns true if the string matches with the string at the start. It returns false if the string doesn’t match with the string at the start.
We can convert the string into uppercase using the toLocaleUpperCase() method according to the host’s current locale.
Example
var x = "Errorsea";
var upr = x.toLocaleUpperCase(); // result: "ERRORSEA"
We can convert the string into uppercase using the toLocaleLowerCase() method according to the host’s current locale.
Example
var x = "Errorsea";
var lwr = x.toLocaleLowerCase(); // result: "errorsea"
This method returns the value of a String object.
Example
var x = 123;
var result = x.toString(); // result: string
Note: It converts the value of the variable to string.
This method is used to returns the primitive value of a String object.
Example
<!DOCTYPE html>
<html>
<body>
<p>Click the button to return the primitive value of the String object.</p>
<button >
Note: The valueOf() method is usually called automatically by JavaScript behind the scenes and not explicitly in code.
Read Also: How to Check the Variable of Type Undefined or Null in JavaScript
Here, we learned the majority of JavaScript string functions with examples. These string functions will help you to perform various operations on strings effortlessly.
I hope you find this article informative.
Enjoy Coding 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…