Use of

In this section, you will learn how to use Tag of JSTL.

Use of

Use of <fn:trim(String)JSTL Tag

     

In this section we will learn how to use <fn:trim> Tag of JSTL. This tag removes white spaces from both ends of specified string. This takes string type as arguments and return string type result.

Syntax :
java.lang.String trim(java.lang.String)

 

 

 

 

 

trim_fnJstlTag.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
    <head>
        <title>Example of fn:trim tag of JSTL</title>
    </head>
    <body>
        <form method="POST">
            This application removes blank spaces from both ends of string.
            <table>
                <tr>
                    <td>Enter string</td>
                    <td><input type="text" name="text"></td>
                </tr>
                
                <tr>
                    <td></td>
                    <td><input type="submit" value="trim"></td>
                </tr>
            </table>  
        </form>
        <c:if test="${pageContext.request.method=='POST'}">
            <c:set var="text" value="${param.text}"/>
            <c:set var="trimText" value="${fn:trim(text)}"/>
            
            <table border="1">
                <tr>
                    <th>Before trim</th>
                    <th>After trim</th>
                    <th>Before trim string length</th>
                    <th>After trim string length</th>
                </tr>
                <tr>
                    <td><c:out value="${text}" /></td>
                    <td><c:out value="${trimText}"/></td>
                    <td><c:out value="${fn:length(text)}" /></td>
                    <td><c:out value="${fn:length(trimText)}" /></td>
                </tr>
            </table>
        </c:if>
    </body>
</html>

This jsp code shows the following output :

Here user will enter string with some white spaces like...... 

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

Download Source Code