Loading Please Wait...
JavaScript strings methods are used to perform various operations on string.
The indexOf() method returns the index (position) of the first occurrence of a string in a string.
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate"); // index = 7
The lastIndexOf() method returns the index (position) of the last occurrence of a string in a string.
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate"); // index = 21
Both indexOf() and lastIndexOf() return -1 if the text is not found.
Both methods accept a second parameter as the starting position for the search.
The lastIndexOf() methods searches backwards (from the end to the beginning), meaning: if the second parameter is 15, the search starts at position 15, and searches to the beginning of the string.
let text = "Please locate where 'locate' occurs!";
let index1 = text.indexOf("locate", 15); // index1 = 21
let index2 = text.lastIndexOf("locate", 15); // index2 = 7
The search() method searches a string for a string (or a regular expression) and returns the position of the match. It is similar to indexOf() except it does not have a start value.
let text = "Please locate where 'locate' occurs!";
text.search("locate");
text.search(/locate/);
The match() method returns an array containing the results of matching a string against a string (or a regular expression).
let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain"); // ain
text.match(/ain/); // ain
text.match(/ain/g) // ain,ain,ain
text.match(/ain/); // ain,AIN,ain,ain
The matchAll() method returns an iterator containing the results of matching a string against a string (or a regular expression).
let text = "I love cats. Cats are very easy to love. Cats are very popular."
text.matchAll("Cats"); // Cats,Cats
text.matchAll(/Cats/g); // Cats,Cats
text.matchAll(/Cats/gi); // cats,Cats,Cats
The includes() method returns true if a string contains a specified value. Otherwise it returns false.
let text = "Hello world, welcome to the universe.";
text.includes("world"); // true
text.includes("world", 12); // false
The startsWith() method returns true if a string begins with a specified value. Otherwise it returns false.
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello"); // true
text.startsWith("world"); // false
text.startsWith("world" , 5); // false
text.startsWith("world", 6); // true
The endsWith() method returns true if a string ends with a specified value. Otherwise it returns false.
let text = "Hello world, welcome to the universe.";
text.endsWith("universe."); // true
text.endsWith("world", 11); // true
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved