Searching And Locating Strings Within Strings
Often times developers need to search within strings to find values. These values are then either returned to the program to display to the end user or return to the application for additional operations. This post will cover indexOf(), lastIndexOf(), and search() methods. In additional we will review their syntax, parameters, and finally examples of the method.
Finding Strings with indexOf()
The first method, indexOf(), can be called on any string object and returns the zero-based index value of the first occurrence of the searchValue passed. If the searchValue passed is not found, the method will return a -1 value.
indexOf() Method Sytax
The method accepts two parameters, searchValue and fromIndex. searchValue is the value that will be used to search against the string that the method is called upon. The fromIndex value is the zero-based index that the search will begin from, the default value is 0, and obviously passing any value larger than string.length will return a -1 value.
var string = “I want to learn more about strings, so I am learning string methods.”; console.log(string.indexOf(“learn”));
The console.log statement will return the value 10. Let’s count off the individual characters to see how we arrived at 10. I,  (space), w, a, n, t,  , t, o  ,l. The first occurrence of the word “learn” begins at the tenth index in the string.
Case Sensitive Note
Finding Strings with lastIndexOf()
lastIndexOf() works exactly like its counterpart indexOf(), however it will return the last occurrence of the value passed in searchValue.
lastIndexOf() Method Syntax
Again, this method accepts two parameters, searchValue and fromIndex.
var string = “I want to learn more about strings, so I am learning string methods.”; console.log(string.lastIndexOf(“learn”));
The console.log statement will return the value 44. It is grabbing the zero-based character index of “learn” within the word “learning”. The method doesn’t care whether the sequence is its own word or not, it only cares if the string has the exact same character combination as what is passed in the methods searchValue parameter. In this case it found that the exact sequence “learn” was in the forty-fourth position in the word “learning”.
Searching Strings with search() Method
The search() string method is a quick and efficient way to search a string for a specific value passed as a parameter in the search() method. The search() method returns the zero-based index, or position, of the first letter of the first occurrence of the the value.
search() Method Syntax
regexp is simply a regular expression object. If a non regular expression object is passed, it is converted to a regular expression. Don’t let this consfuse you if you aren’t familiar with regular expressions. I plan on writing an in depth overview of regular expressions, but for now read more aboutregular expressions on Wikipedia.
var string = “I want to learn more about strings, so I am learning string methods.”; console.log(string.search(“learn”));
The console.log statement will return the value 10.
indexOf() and search() Similarities and Differences
You may have noticed that the returned values for both indexOf() and search() are equal. They both accept the same parameters and return the same values. These two methods are very similar, however with the ability to pass regular expressions as parameters within the search() method, developers can make much more powerful search functions. I like to think of indexOf() as a saw used to work with strings, and the search() method paired with regular expressions as a scalpel. They both help us accomplish a task, however one can be used with incredible precision to help us facilitate tasks that wouldn’t have been achievable with just the indexOf() method.