JavaScript > String Methods

LastIndexOf String in JavaScript

How to search for a particular character or a value in a string from the last occurrence in JavaScript?


LastIndexOf

The lastIndexOf() is an method in strings, which is used to search and return the position from the last occurrence of a specified value in a string.

NOTE: If the search is not found in a string, this method returns "-1" as output.

In JavaScript, by using lastIndexOf() method we can search for a character or a value in a string. The lastIndexOf() method searches from the last occurence (Right to Left) of a specified value in a string.

EXAMPLE: Searching for "TechFunda" in astring and returning the position from the last occurrence.

    <p>Click the below button for searching the character of a string</p>
    <input type="button" value="Search" onclick="Function()" />
    <p id="myId"></p>

    <script>
        function Function() {
            var a = "Visit TechFunda, learn in TechFunda.";
            var r = a.lastIndexOf("TechFunda");
            document.getElementById("myId").innerHTML = r;
        }
    </script>

In the above code snippet we have given ID as "myId" to the second <p> element, we have given the string to the variable "a" in the <script>code. In that string ("Visit TechFunda, learn in TechFunda.") we are searching for the value "TechFunda". For that we used string method lastIndexOf("TechFunda"). Onclick of the button "Search" in HTML code fires the Function() in the <script> code at the same time lastIndexOf()method search for the value "TechFunda" from the last occurence (Right to Left) of a value in a string and gives the numerical value after how many characters the value "TechFunda" is placed in a string.

OUTPUT

From the above output it is cleared that, the value "TechFunda" is placed after 26 characters from the last occurrence in a string.

EXAMPLE: Searching for the character "l" and return the position from the last occurrence.

   <p>Click the below button for searching the character of a string</p>
    <input type="button" value="Search" onclick="Function()" />
    <p id="myId"></p>

    <script>
        function Function() {
            var a = "Visit TechFunda, learn in TechFunda.";
            var r = a.lastIndexOf("l");
            document.getElementById("myId").innerHTML = r;
        }
    </script>

Searching for the character and returning the position from last occurrence

Onclick of the button "Search" in the HTML code fires the Function() in the <script> block, at the same time lastIndexOf("l") method search for the character "l" inside the string and return the position from the last occurrence.

OUTPUT

EXAMPLE: Searching for "Tech" in a string, start the search position at 15.

<p>Click the below button for searching the character of a string</p>
    <input type="button" value="Search" onclick="Function()" />
    <p id="myId"></p>

<script>
    function Function() {
        var a = "Visit TechFunda, learn in TechFunda.";
        var r = a.lastIndexOf("Tech", 15);
        document.getElementById("myId").innerHTML = r;
    }
</script>

Searching for the value with start position

Onclick of the button "Search" in the HTML code fires the Function() in the <script> block, at the same time indexOf("Tech", 15) method search for the value "Tech" in a string by starting the search position at 15 and returns the position from the last occurrence.

OUTPUT

EXAMPLE: In case if the search is not found in a string.

<p>Click the below button for searching the character of a string</p>
    <input type="button" value="Search" onclick="Function()" />
    <p id="myId"></p>

<script>
    function Function() {
        var a = "Visit TechFunda, learn in TechFunda.";
        var r = a.lastIndexOf("z");
        document.getElementById("myId").innerHTML = r;
    }
</script>

Search not found

In the above code snippet we have given value "z" to the lastIndexdexOf() method, the value "z" is not present in the string. So the output returns "-1".

OUTPUT

 Views: 4667 | Post Order: 72



Write for us






Hosting Recommendations