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:
Explanation of the above Program:
- In the above example, we initialized the inputString with “GeeksforGeeks” and stringList by spitting “Geeks for Geeks” spaces.
-
Here, the
${fn:length(inputString)}
evaluates to the length of the string stored in the variableinputString
and prints the output as
13
using <p> tag.
-
The
${fn:length(stringList)}
calculates the number of elements in the collectionstringList
, which is created by splitting the string ‘Geeks for Geeks‘ at spaces.This returns and pints the length as 3.
Recommended Articles
20. JSTL Core Tags
21. JSTL SQL Tags