BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

fn:indexOf() – JSTL Function

Last Updated: December 1, 2013 by Chaitanya Singh | Filed Under: JSTL

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:

fn-indexOf-example

Top Related Articles:

  1. fn:join() and fn:split() JSTL Functions
  2. fn:substring(), fn:substringAfter() & fn:substringBefore() Functions
  3. fn:toUpperCase() – JSTL Function
  4. JSTL <c:out> Core Tag
  5. JSTL <c:url> Core Tag

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSTL Core Tags

  • c:out
  • c:set
  • c:remove
  • c:if
  • c:choose
  • c:when
  • c:otherwise
  • c:catch
  • c:import
  • c:forEach
  • c:forTokens
  • c:param
  • c:url
  • c:redirect
JSTL Functions
  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:indexOf()
  • fn:escapeXML()
  • fn:join() & fn:split()
  • fn:length()
  • fn:startsWith()
  • fn:endsWith()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:trim()
  • fn:toUpperCase()
  • fn:toLowerCase()
  • fn:replace()

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap