In this post, I will help you undestand how to use the <fn:indexOf> tag in the JSTL function tags library with code example.

The fn:indexOf function returns the index of the first occurrence of a substring in a given string. The index starts at ‘0’.

 

JSTL <fn:indexOf> Syntax:

${fn:indexOf(<var containing string to search for substring>,<substring>)}

 

JSTL <fn:indexOf> Example:

The following code snippet searches for a substring ‘JSTL’ and prints its index in a given string.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>fn:indexOf Demo</title>
  </head>
  <body>
	<h1>fn:indexOf Demo</h1>

	<c:set var="text" value="The JSTL Functions Are Great." />
	<c:out value="${text}"/>
	<p>Index of 'JSTL' from above text is: ${fn:indexOf(text,'JSTL')}</p>
  </body>
</html>

 

Output:

JSTL function fn-indexOf

 

Other JSTL Function Tags:

contains  |  containsIgnoreCase  |  endsWith |  escapeXml  |  join  |  length  |  replace  |  split  |  startsWith  |  substring  |  substringAfter  |  substringBefore  |  toLowercase  |  toUpperCase  |  trim


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment