Javascript String Methods


Javascript provides multiple properties and methods to perform operations on string values.


STRING METHODS AND PROPERTIES

  • In JavaScript, methods and properties are easily applicable to unique primitive values.
  • JavaScript manages primitive values as objects while applying methods and properties.

STRING LENGTH


length property is used to check the length of a string.


var str = "This is a random string";
var str_len = str.length; //str_len will have the string length

Live Demo!


LOCATING A STRING IN A STRING


The indexOf() method is used to fetch the location of a specific string within a string. Take a look at the syntax of indexOf():


var str = "This is also a random string.";
var located_at = str.indexOf("also");   //located_at = 8
//'also' is located at 8th index

Live Demo!


The lastIndexOf() method is used to fetch the index of the last occurrence of a text in a string.



var str = "this will occur twice and this is a string";
var pos = str.lastIndexOf("this");    //Output will be 26

Live Demo!


SEARCHING FOR A STRING IN A STRING


String.search()


The string search() method will search the string for specific text and return its position.



let myStr = "Where can you find the word 'find'?";
document.write(myStr.search("find"));  // Outputs 14

Live Demo!


At first the indexOf() and search() methods seem to be the same but there are differences between the two.


  • The indexOf() method cannot take regular expressions as search values but search() can.
  • The search() method does not have any possible second parameter for the starting position.

String.match()


The string match() method is used to search for a match in the string against a regular expression. The matches are returned as an array object.



let myStr = "Find in the string.";
document.write(myStr.match(/in/g));  // Outputs an array [in,in,in]

Live Demo!


String.includes()


The includes() method checks if specified text is present in the provided string. If it is, true is returned, otherwise false.



let myStr = "Find in the string.";
document.write(myStr.includes("the")); //Outputs true

Live Demo!


We can also provide a starting point for the search, as parameter to the method.



let myStr = "Find in the string.";
document.write(myStr.includes("the", 9)); //Outputs false

Live Demo!


String.startsWith()


The startsWith() method checks if a specific value is the start of a string. If it is, true is returned, otherwise false.

This method can also take a starting point as an optional parameter value.



let myStr = "Find in the string.";
document.write(myStr.startsWith("Find")); //Outputs true

Live Demo!



let myStr = "Find in the string.";
document.write(myStr.startsWith("string", 12)); //Outputs true

Live Demo!


String.endsWith()


The endsWith() methods checks if a string ends in the specified value. If it does, true is returned, otherwise false.


let myStr = "Hello World";
document.write(myStr.endsWith("World")); //Outputs true

Live Demo!


the endsWith() method can take a second parameter value which is for the length of the string to check.


let myStr = "Hello World. This is a test.";
document.write(myStr.endsWith("World", 11)); //Outputs true

Live Demo!


EXTRACTING STRING PARTS


Following are some methods which are used to extract a sub-string from a string.

THE slice() METHOD


The slice() method extracts a part of a string and restores it as a new string, It takes the beginning position and the ending position (End position is not included).


Example:


var str = "Tomato, Eggplant, Youtube";
var res = str.slice(8, 16);

Live Demo!


In the case of negative parameter, the arrangement is calculated from the end of the string.


THE substring() METHOD


The substring() method is same as slice() method. However, the main difference is that substring() can’t accept negative indexes.


Example:


var str = "Potato, Eggplant, Tomato";
var res = str.substring(8, 16);

Live Demo!

If you exclude the 2nd parameter, this method will slice out the remaining string.


THE substr() METHOD:


The substr() method is same as slice() method. However, the main difference is that the 2nd parameter identifies the length of the removed part.


Example:


var str = "Potato, Eggplant, Tomato";
var res = str.substr(8, 8);  //The 2nd argument is the length of the extracted part

Live Demo!