Open In App

JSTL fn:indexOf() Function

For JSTL-based applications, fn:indexOf() function is used to get the first occurrence index or the position of the substring in the main string. This function-

In this article, we will see the syntax of this function its parameters, and the example with the output and explanation.



Syntax of fn:indexOf() Function

${fn:indexOf(string, substring)}

where,

Example of JSTL fn:indexOf() Function

Below is the implementation of JSTL fn:indexOf() Function:






<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
<html>
  <head>
      <title>JSTL fn:indexOf() Example</title>
  </head>
  <body>
    <c:set var="inputString" value="Welcome to GeeksforGeeks"/>
    <c:set var="substringToFind" value="Geeks"/>
    <h3>Original String: ${inputString}</h3>
    <h3>Substring to Find: ${substringToFind}</h3>
      
        <c:set var="indexOfSubstring" value="${fn:indexOf(inputString, substringToFind)}"/>
      
    <h3>Index of Substring: ${indexOfSubstring}</h3>
  </body>
</html>

Output:

Original String: Welcome to GeeksforGeeks
Substring to Find: Geeks
Index of Substring: 11

Output Screen of JSTL fn:indexOf() Function:

Explanation of the above Program:

Article Tags :