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 and JSTL Tag

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)

 

substringBefore_subStringAfter_JstlTag.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
    <head>
        <title>Example of 'fn:substringBefore' and 'fn:substringAfter' tag of jstl</title>
    </head>
    <body>
        <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.
            <table>         
                            <tr>
                    <td>Enter First String</td>
                    <td><input type="text" name="fString"></td>
                </tr>
                
                                <tr>
                    <td>Enter Second String</td>
                    <td><input type="text" name="sString"></td>
                </tr>
                
                                <tr>
                    <td></td>
                    <td><input type="radio" name="radio" value="before"> Before
                    <input type="radio" name="radio" value="after"> After </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="cut"></td>
                </tr>
            </table>  
        </form>
        <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>
                <c:if test="${param.radio=='after'}">

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

                </font>
            </c:if>
        </c:if>
    </body>
</html>

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