Open In App

JSTL fn:length() Function

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

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.

  • Used to retrieve the size or length information within the JSTL expressions.
  • The fn:length() function is part of the JSTL core tag library.

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,

  • ${fn:length(...)}: This is the JSTL expression for getting the length or size.
  • String inputString: This is the paramter passed to the length function. The length function retrins the length of the spcifiec string.
  • Collection object: This is the paramter passed to the length function. The length fuction retrins the number of elements in the collection.

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.

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: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:

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads