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:substring(), fn:substringAfter() & fn:substringBefore() Functions

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

In this tutorial we will discuss fn:substring (), fn:substringAfter() and fn:substringBefore() functions of JSTL. All of these functions are used for getting a part of the string from a given input string. The way of getting the output is different in all three functions.

fn:substring ()

This function returns a substring of given input string as per the given start and end position.

Syntax

String fn:substring(String inputstring, int start, int end)
  • Return type of function: String
  • inputstring: The string from which a substring needs to be taken
  • start: Starting position of substring
  • end: end position of substring

Example – fn:substring() function

In this example we are fetching a substring from a given string  by providing the starting and end positions of the substring.

<%@ 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:substring() example</title>
</head>
<body>
<c:set var="msg" value="This is an example of JSTL function"/>
${fn:substring(msg, 10, 26)}
</body>
</html>

Output:

substring-from-given-string

fn:substringAfter()

It returns the part of a given string which lies after a provided string value.

Syntax

String fn:substringAfter(String input, String afterstring)

Whatever is present in the input after the “afterstring” is being returned by this function. Refer the below example to have the more clarity on this topic.

Example of fn:substringAfter()

<%@ 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:substringAfter() example</title>
</head>
<body>
<c:set var="name" value="Rahul Pratap Singh"/>
${fn:substringAfter(name, "Pr")}
</body>
</html>

Output:

substring-after-given-string

fn:substringBefore()

It is just opposite of fn:substringAfter function. It returns the the part of original string which lies before a specified string value.

Syntax

String fn:substringBefore(String input, String beforestring)

The part of “input” before the “beforestring” will be returned as the output of this function

Example of fn:substringBefore()

<%@ 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:substringBefore() example</title>
</head>
<body>
<c:set var="justastring" value="Hi, How are you??"/>
${fn:substringBefore(justastring, "are")}
</body>
</html>

Output:

substring-before-a-given-term

Top Related Articles:

  1. fn:join() and fn:split() JSTL Functions
  2. fn:toUpperCase() – JSTL Function
  3. JSTL <c:out> Core Tag
  4. JSTL <c:url> Core Tag
  5. JSTL <c:import> 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