Please enable Javascript to correctly display the contents on Dot Net Tricks!
 
Ready to master C#, .NET, MVC, JAVA, PHP, AngularJS, Hadoop, Android, iphone, Testing skills?
Join our online/offline training programs and take your career to the next level! For more details call us +91-9871749695 or write us info@dotnet-tricks.com

How to find a string within a jQuery or javascript string

Posted By : Shailendra Chauhan, 13 Apr 2013
Total Views : 26,383   
 
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.

 
Further Reading
 
About the Author
Hey! I'm Shailendra Chauhan full-time author, consultant & trainer. I have more than 6 years of hand over Microsoft .NET technologies and other web technologies like JavaScript, AngularJS, NodeJS etc. I am an entrepreneur, the founder & chief editor of www.dotnet-tricks.com and www.dotnettricks.com. I am author of most popular e-books for technical Interview on ASP.NET MVC Interview Questions and Answers & AngularJS Interview Questions and Answers & LINQ Interview Questions and Answers.
I have delivered 100+ training sessions to professional world-wide over Microsoft .NET technologies such C#, ASP.NET MVC, WCF, Entity Framework and other mobile technologies such Ionic, PhoneGap, Corodva. Read more...
 
Free Interview Books
 
SUBSCRIBE & FOLLOW US
 
Browse By Category
 
 
Like us on Facebook