Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery String indexOf Method Example in JavaScript (Uses of indexof Method in jQuery)

Oct 14, 2014
Introduction

Here I will explain what is string indexof method in JavaScript or
jQuery and uses of indexof method in JavaScript or jQuery with example. Indexof method in jQuery or JavaScript is used to check whether particular word existing in string or not and we can get position of first occurrence of word in string. This indexof method will return -1 incase if the word not exists in string in JavaScript or jQuery.


Declaration of indexof method

Generally we will use indexof function like as shown below

var str = 'welcome to aspdotnet-suresh.com';
var strval = str.indexOf('aspdotnet');
if (strval > 0) {
alert('word exists');
}
If you want to check it in complete example we need to write the code like as shown below

Sample1:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Indexof String</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$('#btncheck').click(function() {
var str = 'welcome to aspdotnet-suresh.com';
var strval = str.indexOf($('#txtword').val());
if (strval > 0) {
alert('word exists');
}
else {
alert('not exists')
}
})
})
</script>
</head>
<body>
<form id="form1">
<div>
<b>Enter Text:</b><input type="text" id="txtword" value="aspdotnet" />
<input type="button" id="btncheck" value="Check" />
</div>
</form>
</body>
</html>
Live Demo

To check live demo try to click on below button to check whether particular word exists or not in string 'welcome to aspdotnet-suresh.com'


Enter Text:
Sample2:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Indexof String</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$('#btncheck').click(function() {
var str = 'welcome to aspdotnet-suresh.com';
var strval = str.indexOf($('#txtword').val());
alert('Starting Position of Word is '+strval)
})
})
</script>
</head>
<body>
<form id="form1">
<div>
<b>Enter Text:</b><input type="text" id="txtword" value="aspdotnet" />
<input type="button" id="btncheck" value="Get Word Position" />
</div>
</form>
</body>
</html>
Live Demo

To check live demo try to click on below button to get stating position of particular word in string 'welcome to aspdotnet-suresh.com'


Enter Text:

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.