swissmisfit

JavaScript: Learning String Search Methods

Javascript String Search Methods: Inspecting Strings

JavaScript String Search Methods

String search methods are pre-defined methods built into Javascript that help developers find, edit, manipulate, and work with strings more easily. String search methods provide a number of very handy pre-built functions that drastically increase development efficiency while limiting the amount of code needed to perform actions.

If you haven’t had a chance to read my short post on Javascript strings, properties, special characters, and escaped sequences, I highly recommend it, it may help in your understanding of some of the search methods defined below. Additionally, if you are unfamiliar with zero-based numbering [0],[1], etc. check out the wikipedia articlezero-based numbering for more details. Zero-based numbering is a common theme in most development languages, and understanding it is critical to understanding string search methods.

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

string.indexOf(searchValue, fromIndex)

Parameters

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.

indexOf() Example

var string = “I want to learn more about strings, so I am learning string methods.”; console.log(string.indexOf(“learn”));

Return Value

The console.log statement will return the value 10. Let’s count off the individual characters to see how we arrived at 10. [0]I, [1] (space), [2]w, [3]a, [4]n, [5]t, [6] , [7]t, [8]o [9] ,[10]l. The first occurrence of the word “learn” begins at the tenth index in the string.

Case Sensitive Note

It may also be worth noting that this method along with many other methods in JavaScript are case sensitive. This means that in our above example a searchValue of “Learn” would return a -1 value for the method call. The reason being is that “Learn” does not equal “learn” in respect to the method.

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

string.lastIndexOf(searchValue, fromIndex)

Parameters

Again, this method accepts two parameters, searchValue and fromIndex.

lastIndexOf() Examples

var string = “I want to learn more about strings, so I am learning string methods.”; console.log(string.lastIndexOf(“learn”));

Return Value

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

string.search(regexp)

Parameters

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.

search() Examples

var string = “I want to learn more about strings, so I am learning string methods.”; console.log(string.search(“learn”));

Return Value

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.

Tagged with: , ,
Posted in Web Development

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Recent Projects
  • Grub.ly
    Explore and discover new foods!
    Comments

  • Grub.ly
    Explore and discover new foods!
    Comments

About Me Dal Price: SwissMisfit

I’m Dal, an enthusiast for all things technology, design, music, and food. This is a collection of inspirations, misfit thoughts, and random musings that may or may not be interesting to others.

Other Places to find me
  • RSS
    Feed updates from the blog

  • Twitter
    Bits of random 140 characters

  • Flickr
    So many pruty pictures!

  • XBox
    Pwning N00bs

  • Instagram
    Filters make my pictures better