Home Jsp Use of and JSTL Tag

Related Tutorials


Share on Google+Share on Google+

Use of and JSTL Tag

In this section, you will learn how to use and Tag of JSTL. These tags returns the sub string before/after specified sub string.

Use of <fn:substringBefore(String, String)> and <fn:subStringAfter(String, String)>JSTL Tag


In this section we will learn how to use <fn:substringBefore> and <fn:substringAfter> Tag of JSTL. These tags returns the sub string before/after specified sub string. This takes string type as arguments and return string type result.

<fn:substringBefore> This tag returns a subset of a string before a specific substring. <fn:substringAfter> This tag returns a subset of a string following a specific substring.



Syntax :
java.lang.String substringBefore(java.lang.String, java.lang.String)

java.lang.String substringAfter(java.lang.String, java.lang.String)



<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
        <title>Example of 'fn:substringBefore' and 'fn:substringAfter' tag of jstl</title>
        <form method="POST">
         Enter first string from which you want to retrieve substring.<br>
   Enter second string before/after that you want to retrieve subString.
                    <td>Enter First String</td>
                    <td><input type="text" name="fString"></td>
                    <td>Enter Second String</td>
                    <td><input type="text" name="sString"></td>
                    <td><input type="radio" name="radio" value="before"> Before
                    <input type="radio" name="radio" value="after"> After </td>
                    <td><input type="submit" value="cut"></td>
        <c:if test="${pageContext.request.method=='POST'}">
                        <c:set var="fString" value="${param.fString}"/>
             <c:set var="sString" value="${param.sString}"/>
            <font size="5" color="green">
                <c:if test="${param.radio=='before'}"> 
                    <c:out value="${fn:substringBefore(fString,sString)}"/>  
                <c:if test="${param.radio=='after'}">

<c:out value="${fn:substringAfter(fString,sString)}"/> 


This jsp code shows the following output :

Here user will enter string and a sub string and then select radio button as per user choice.... 

When user clicks on cut button, output will be.......

Download Source Code


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 24, 2008

Related Tutorials

Discuss: Use of and JSTL Tag  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image