Find a Character or a Substring Within a String
By Stephen Bucaro
The Java Script String object provides several methods that allow
us to search for a character or substring within a string. For example, the
code below finds the word "easy" in the sentence "code is easy to read".
var strSentence = "code is easy to read";
var i = strSentence.indexOf("easy");
if(i < 0) alert("easy not found");
else alert("easy found at index: " + i);
After execution of the code, the variable i would contain the value 8. When the
indexOf method cannot find the indicated substring in the string, it returns
the value -1. As you see in the code above, we check for i being less than zero and
then produce the proper message.
What if the sentence contains the word "easy" twice and we want to check only
for the second one? In that case, we would provide the indexOf method with a starting
index from which to begin the search.
var strSentence = "this easy code is easy to read";
var i = strSentence.indexOf("easy", 10);
if(i < 0) alert("easy not found");
else alert("easy found at index: " + i);
The code above starts searching for the substring at index 10, which is past the
first substring "easy".
The string object also provides the lastIndexOf method that will search for
a substring starting from the end of the string.
var strSentence = "this easy code is easy to read";
var i = strSentence.lastIndexOf("easy");
if(i < 0) alert("easy not found");
else alert("easy found at index: " + i);
The code shown above will return the same index as the previous example, without
requiring a starting index. Even though it searches for a substring starting
from the end of the string, it still returns the index relative to 0 being at the
beginning of the string.
What if you want to check an email address to make sure that it does not have a
dot at the end? That would indicate an invalid email address. You could use the
String object's length property to determine the length of the email
address string. Then use the lastIndexOf method with the length as a starting
index to retrieve the last character of the email address.
var strEmail = "name@domain.com";
var len = strEmail.length;
var i = strEmail.lastIndexOf(".", len);
if(i == len - 1) alert("Invalid Email Adress");
else alert("Valid Email Adress");
Note in the code above that we subtract one from the length of the string before
we compare it to the index of the dot. Remember, because the indexes start with
0, the length of the string will be one more than the highest index in the string.
Try the code above with and without the characters "com" on the end of the email address.
More Java Script Code: • Convert a Number to a String • Window onload Event • Java Script prompt Message Box • The Document Object Model (DOM) • Java Script confirm Message Box • Use moveBy Method to Move Window by a Specific Offset • Java Script alert Message Box • Rounding a Number with JavaScript • Determine Absolute Value • Java Script Quick Reference : JavaScript toString Method Converts a Number to a String
|