This post helps you understand how to use the <fn:join> tag in the JSTL function tags library with code example.

The fn:join function joins the elements of an array into a string with specified delimiter.

 

JSTL <fn:join> Syntax:

${fn:join([array to join], <separator>)}

 

JSTL <fn:join> Example:

The following JSP code example splits a string delimited by comma (,) and then uses the resulted array to combine the array elements separated by empty space.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>fn:join Demo</title>
  </head>
  <body>
	<h1>fn:join Demo</h1>

	<c:set var="numbers" value="One,Two,Three,Four,Five" />
	<c:set var="splitNumbers" value="${fn:split(numbers,',')}" />
	<c:set var="joinedNumbers" value="${fn:join(splitNumbers,' ')}" />
	<p>Numbers before join: ${numbers}</p>
	<p>Joined Numbers: ${joinedNumbers}</p>
  </body>
</html>

 

Output:

JSTL function fn-join

 

Other JSTL Function Tags:

contains  |  containsIgnoreCase  |  endsWith |  escapeXml  |  indexOf  |  length  |  replace  |  split  |  startsWith  |  substring  |  substringAfter  |  substringBefore  |  toLowercase  |  toUpperCase  |  trim


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment