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".
Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Find a Character or a Substring Within a String

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.

RSS Feed RSS Feed



Follow Stephen Bucaro Follow @Stephen Bucaro


Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268