JSTL fn:length() Function

In this tutorial you will learn about the JSTL fn length function.

JSTL Length function is used to get the size of a collection or length of a String.

length function, however the size() method is defined by the java.util.Collection interface can not be accessed through the JSP EL because the JavaBeans component design pattern is not confirmed by it for the properties, is used for finding out the how many characters (including the white space) are there in a string or how many items are there in a collection.

Syntax :
The fn:length() function has following syntax:

int length(java.lang.Object)

JSTL Length Function Example:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL Length Function Example</title>
</head>
<body>
  <c:out value="${fn:length('Hello')}"></c:out>
</body>
</html>

Output: 5

As you can see above, JSTL Length Function takes ‘Hello’ string as an input parameter and will return 5 as a result.

<%@ 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>Using JSTL Functions</title>
</head>
<body>

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="This is second String." />

<p>Length of String (1) : ${fn:length(string1)}</p>
<p>Length of String (2) : ${fn:length(string2)}</p>

</body>
</html>

This would produce following result:

Length of String (1) : 21
Length of String (2) : 22

<<Previous <<   || Index ||   >>Next >>
Previous
Next