Open In App

JSTL fn:length() Function

In JSTL, the fn:length() function is mainly used to specify the length or size of a collection, array, or string in JSP. This function returns the number of elements in the given object as input.

In this article, we will see the syntax, parameters, and example of the fn:length() function.



Syntax of fn:length()

${fn:length(String inputString/Collection object)}

Where,

Example of JSTL fn:length() Function

In this example, we see how to find the length of a string using the JSTL fn:length() Function.






<%@ 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:length() example</title>
  </head>
  <body>
    <c:set var="inputString" value="GeeksforGeeks"/>
    <c:set var="stringList" value="${fn:split('Geeks for Geeks', ' ')}"/>
    <p>Length of the String: ${fn:length(inputString)}</p>
    <p>Length of the Collection: ${fn:length(stringList)}</p>
  </body>
</html>

Output:

Length of the String: 13
Length of the Collection: 3

Output Screen of the above Program:

Explanation of the above Program:

  1. In the above example, we initialized the inputString with “GeeksforGeeks” and stringList by spitting “Geeks for Geeks” spaces.
  2. Here, the ${fn:length(inputString)} evaluates to the length of the string stored in the variable inputString and prints the output as 13 using <p> tag.
  3. The ${fn:length(stringList)} calculates the number of elements in the collection stringList, which is created by splitting the string ‘Geeks for Geeks‘ at spaces. This returns and pints the length as 3.

Article Tags :