Java jstl function indexof
The JSTL (JavaServer Pages Standard Tag Library) fn
tag library provides a set of functions for manipulating strings, collections, and other objects in JSP (JavaServer Pages) pages. One of the functions in the fn
tag library is the fn:indexOf
function, which is used to find the index of the first occurrence of a substring within a string.
The fn:indexOf
function takes two arguments: the string to search and the substring to find. The function returns the index of the first occurrence of the substring within the string, or -1 if the substring is not found. The syntax of the function is as follows:
${fn:indexOf(string, substring)}
where string
is the string to search, and substring
is the substring to find.
For example, to find the index of the substring "world" within the string "hello world", you would use the following expression:
${fn:indexOf('hello world', 'world')}
This expression returns 6
, which is the index of the first occurrence of the substring "world" within the string "hello world".
You can also use variables instead of literals for the string
and substring
arguments. For example, if you have variables text
and substring
containing strings, you can find the index of the substring within the string using the following expression:
${fn:indexOf(text, substring)}
This expression returns the index of the first occurrence of the substring within the string in the variable text
.
Note that the fn
tag library must be imported at the beginning of the JSP page using the following tag:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
This imports the fn
tag library and assigns it a prefix of fn
.