Home
Node.js
String indexOf and lastIndexOf
This page was last reviewed on Dec 27, 2023.
Dot Net Perls
IndexOf. Suppose one string exists as a substring inside another. In Node.js, we can search a string with indexOf or lastIndexOf—an index integer is returned.
Another method, includes, also can search for strings. For more complex requirements a regular expression pattern and search() can be used.
IndexOf example. This method searches strings—we pass it a substring we want to find in a string. It returns the index where the substring is found. If nothing is found it returns -1.
Here We have a string that contains the final 3 letters of the alphabet. Then we search for 2 of them—and "q" which is not found.
var letters = "xyz"; // This is at index 0. var letterX = letters.indexOf("x"); console.log("RESULT 1: " + letterX); // This is at index 2. var letterZ = letters.indexOf("z"); console.log("RESULT 2: " + letterZ); // Not found, so we get -1 instead of an index. var letterQ = letters.indexOf("q"); console.log("RESULT 3: " + letterQ);
RESULT 1: 0 RESULT 2: 2 RESULT 3: -1
Start argument. The indexOf method has an optional second argument. This is an integer that tells indexOf where to start searching.
Here We pass 0 and the first "cat" string is found. But when we pass 1, we find the second "cat" string as we miss the first.
var words = "cat dog cat"; // Find the word beginning at index 0. var result1 = words.indexOf("cat", 0); console.log("RESULT: " + result1); // Find the word beginning at index 1. // ... The first instance is not found. var result2 = words.indexOf("cat", 1); console.log("RESULT: " + result2);
RESULT: 0 RESULT: 8
LastIndexOf. This method searches a string from the right side. It starts with the last index and then processes backwards to the first index.
var words = "cat dog cat"; // Find last occurrence of this substring. var result = words.lastIndexOf("cat"); console.log("LASTINDEXOF: " + result);
LASTINDEXOF: 8
Includes. Sometimes an index is not needed when searching a string. We use includes() to see whether a substring exists as part of another or not.
Tip This could be implemented with indexOf and a check against -1. But includes is easier to read.
var birds = "parakeet parrot penguin"; // See if the string includes parrot. if (birds.includes("parrot")) { console.log("Parrot found"); } // This word is not found in the string. if (!birds.includes("zebra")) { console.log("Zebra not found"); }
Parrot found Zebra not found
Scan strings. With indexOf, we must loop over the string to find a single value. This can result in excessive looping. We can scan strings for multiple characters at once with charCodeAt.
String charAt
Review. Strings are commonly searched in Node. With indexOf and its friends lastIndexOf and includes, this searching is made easy.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 27, 2023 (image).
Home
Changes
© 2007-2024 Sam Allen.