Please enable Javascript to correctly display the contents on Dot Net Tricks!
 
Coding is Rhyme
Handy Tricks For Beginners & Professionals
jQuery - Tutorial, Article, Archive, Pdf, Handy Tricks, Training, Code Snippets, Reference Manual, Objects, Attributes, Selectors, Events, Functions, CSS, DOM Manipulation, AJAX Support, Drag and Drop, Effects, Event Handling, UI, Plug-ins

How to find a string within a jQuery or javascript string

Posted By : Shailendra Chauhan, 13 Apr 2013
Keywords : jquery string contains function,find a substring in a string in javascript or jquery,finding sub string in jquery code form dynamic string

Sometimes, you required to find a the existence of a small string with in a string. This article will demonstarte, how could you do by using plain JavaScript.

First Method

//returns -1 if doesn't contains
if (str.indexOf("Hello") >= 0) 
{
 //string contains Hello
}
//OR
//For your are not sure about the case of the string
//returns -1 if doesn't contains
if (str.toLowerCase().indexOf("hello") >= 0)
{
 //string contains hello
}
This indexOf() method will return the position of the match, or -1 if it isn’t found.

Second Method

//returns false if doesn't contains
if (/hello/i.test(str))
{
 //string contain Hello
}
What do you think?

I hope you will enjoy the tips while playing with JavaScript. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

About the Author
Shailendra Chauhan works as Sr. Software Engineer at reputed MNC and has more than 5 years of hand over Microsoft .NET technologies. He is a .NET Consultant and is the founder & chief editor of www.dotnet-tricks.com and www.dotnetinterviewtricks.com blogs. He is an author of books ASP.NET MVC Interview Questions and Answers & LINQ Interview Questions and Answers.
He loves to work with web applications and mobile apps using Microsoft technology including ASP.NET, MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery, jQuery Mobile, Knockout.js, Angular.js and many more web technologies. More...
 
 
Recommended Articles!
 
Subscribe & follow Us
 
Creative Commons License
Dot Net Tricks is licensed under a Creative Commons Attribution 4.0 International License.
Free .NET Interview Books
 
Learn In Hindi
Browse By Category
 
Recent Articles
 
Popular Articles
 
Like us on Facebook
 
Featured AD