fn:indexOf() function is used for finding out the start position (index) of a string in the provided string.
Syntax
int indexOf(String, String )
The return type of this function is int. It returns the starting position (or index) of the second string (second argument of the function) in the first string (first argument of the function).
Points to Note:
- The function returns -1 when the string is not found in the input string.
- Function is case sensitive. It treats uppercase and lowercase character of same alphabet as different.
- It returns the index of first occurrence which means if the string is present in input more than once than the function would return the index of first occurrence. Refer example.
Example of fn:indexOf() function
In this example we are finding out the index of few strings and displaying them using EL.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <html> <head> <title>JSTL fn:indexOf() example</title> </head> <body> ${fn:indexOf("My name is Chaitanya Singh", "chaitanya")} ${fn:indexOf("My name is Chaitanya Singh", "Chaitanya")} ${fn:indexOf("This is an example", "is")} ${fn:indexOf("JSTL function - indexOf function", "function")} </body> </html>
Output:
Leave a Reply