JavaScript > String Methods

IndexOf String in JavaScript

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


IndexOf

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

NOTE: In case if the search is not found inside the string, this method returns "-1".

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

EXAMPLE: Searching for "code" in a string and returning the position from first occurence.

    <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 = "Loop is used to repeat a code block till a certain condition is true.";
            var r = a.indexOf("code");
            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 block. In that string ("Loop is used to repeat a code block till a certain condition is true") we are searching for the value "code". For that we used string method indexOf("code").

Onclick of the button "Search" in HTML code fires the Function() in the script block at the same time indexOf() method search for the value "code" from the first occurence (Left to right) of a value in a string and gives the numerical value after how many characters the value "code" is placed in a string.

OUTPUT

From the above output it is cleared that, from the first occurrence the value "code" is placed after 25 characters in a string.

EXAMPLE: Searching for the character "i" and returning the position from the first occurence.

<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 = "Loop is used to repeat a code block till a certain condition is true.";
        var r = a.indexOf("i");
        document.getElementById("myId").innerHTML = r;
    }
</script>

Searching for character

Onclick of the button "Search" in the HTML code fires the Function() in the script block, at the same time indexOf("i") method search for the character "i" in a string and returns the position from the first occurrence.

OUTPUT

EXAMPLE: Searching for the character "i" in a string, start the search position at 12.

<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 = "Loop is used to repeat a code block till a certain condition is true.";
        var r = a.indexOf("i", 12);
        document.getElementById("myId").innerHTML = r;
    }
</script>

Searching for the character wit the start position

Onclick of the button "Search" in the HTML code fires the Function() in the script block, at the same time indexOf("i", 12) method search for the character "i" in a string by starting the search position at 12 and returns the position from the first occurrence.

OUTPUT

EXAMPLE: In case if the search value or character 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 = "Loop is used to repeat a code block till a certain condition is true.";
        var r = a.indexOf("z");
        document.getElementById("myId").innerHTML = r;
    }
</script>

Search not found

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

OUTPUT

 Views: 5432 | Post Order: 71



Write for us






Hosting Recommendations