BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

fn:join() and fn:split() JSTL Functions

Last Updated: December 2, 2013 by Chaitanya Singh | Filed Under: JSTL

In this tutorial we will see fn:join() and fn:split() functions of JSTL.

fn:join()

It concatenates the strings with a given separator and returns the output string.

Syntax

String fn:join(String arrayofstrings, String separator)

It concatenates all the elements of the input array along with the provided separator in between. The return type of this function is String, it returns the output string after concatenation.

Example – Join strings using fn:join() function

In this example we are having an array of strings and we are joining them using the separator (‘ & ‘).

<%@ 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:join() example</title>
</head>
<body>
<%
String arr[]={"Chaitanya", "Rahul", "Ajeet"};
session.setAttribute("names", arr);
%>
${fn:join(names, " & ")}
</body>
</html>

Output:

As you can see all the three names got concatenated having ‘ &’ in between.

strings-join-example

fn:split()

It splits a given string into an array of substrings. Splitting process considers the delimiter string which we provide during function call. I.e. we provide the string and delimiter as arguments to the function and it returns the array of strings after splitting the input based on the delimiter string.

Syntax

String[] fn:split(String inputstring, String delimiterstring)

Example – Join strings using fn:split() function

In this example we are having a input string which has few space characters in between. We have split the string using space as delimiter using fn:split() function. The function returned the array of the sub-strings.

<%@ 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:split() example</title>
</head>
<body>
<c:set var="msg" value="This is an example of JSTL function"/>
<c:set var="arrayofmsg" value="${fn:split(msg,' ')}"/>
<c:forEach var="i" begin="0" end="6">
 arrayofmsg[${i}]: ${arrayofmsg[i]}<br>
</c:forEach>
</body>
</html>

Output:

splitting-of-string

Top Related Articles:

  1. fn:toUpperCase() – JSTL Function
  2. fn:substring(), fn:substringAfter() & fn:substringBefore() Functions
  3. JSTL <c:choose>, <c:when>, <c:otherwise> Core Tags
  4. JSTL <c:out> Core Tag
  5. JSTL <c:url> Core Tag

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSTL Core Tags

  • c:out
  • c:set
  • c:remove
  • c:if
  • c:choose
  • c:when
  • c:otherwise
  • c:catch
  • c:import
  • c:forEach
  • c:forTokens
  • c:param
  • c:url
  • c:redirect
JSTL Functions
  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:indexOf()
  • fn:escapeXML()
  • fn:join() & fn:split()
  • fn:length()
  • fn:startsWith()
  • fn:endsWith()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:trim()
  • fn:toUpperCase()
  • fn:toLowerCase()
  • fn:replace()

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap