JSTL Function fn:indexOf Example
- Details
- Written by Nam Ha Minh
- Last Updated on 31 August 2019   |   Print Email
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:
Other JSTL Function Tags:
contains | containsIgnoreCase | endsWith | escapeXml | join | length | replace | split | startsWith | substring | substringAfter | substringBefore | toLowercase | toUpperCase | trim
Comments