Loading ...

How to find last index of a character in javascript | CodeAsp.Net

How to find last index of a character in javascript

 /5
0 (0votes)

In this blog I will explain how to find last index of character in string with javascript. To explain that I have used lastIndexOf method of javascript.I have used on button and one text box. In below example the character to find is comma (‘,’).On click of button an alert will pop up which will display the index of last index of specified character. Below is the code :

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

</head>

<body>

<div>

<input id="txtText" type="text" /><br />

<input id="txtButton" type="button" value="Test" onclick="testfunction();" />

</div>

</body>

<script type="text/javascript">

var searchCharacter=',';

function testfunction()

{

var txtText=document.getElementById('txtText');

var txtTextValue=txtText.value;

var index=txtTextValue.lastIndexOf(searchCharacter);

alert(index);

}

</script>

</html>

 

 

 

Happy Reading. !

 

Comments (no comments yet)

Top Posts