Open In App

JSTL fn:trim() Function

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In JSTL the fn:trim() function is used to remove the leading and trailing whitespaces in the given input or specified string. The fn:trim() function also helps in properly cleaning up and standardizing the formatting of strings within the JSP.

In this article, we will see the detailed syntax and the parameters of this function. We will also see the practical implementation in terms of examples.

Syntax of fn:trim() function

${fn:trim(string)}

Where,

  • ${fn:trim()}: JSTL expression used to remove the blank spaces from both ends of a given string.
  • string: main input string value from which the whitespaces will be removed.

Example of fn:trim() Function

In this, we will see the use of fn:trim() in the JSP code example:

HTML




<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ 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:trim() Example</title>
  </head>
  <body>
    <c:set var="inputString" value="   GeeksforGeeks   "/>
    <h3>Original String: "${inputString}"</h3>
    <p>Length before trim: ${fn:length(inputString)}</p>
      
    <p>Trimmed String: "${fn:trim(inputString)}"</p>
      
    <p>Length after trim: ${fn:length(fn:trim(inputString))}</p>
  </body>
</html>


Output

Original String:   GeeksGeeks
Length before trim: 15
Trimmed String: GeeksforGeeks
Length after trim: 13

Output Screen of the JSTL fn:trim() Function:

Output for above example of fn:trim()

Output

Explanation of the above Program:

  • inputString variable is initialized using the value ” GeeksforGeeks “. This inputString contains the whitespaces at both ends.
  • Then we print the original string and also the length before trimming the input string.
  • By using the function trim(), we are removing the whitespaces from both ends of the inputString variable.
  • Once again, we are printing the value of the trimmed string with the updated length.
  • In the output, we can see that the length is been reduced, which indicates that the whitespaces are removed.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads