beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

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

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

Enjoyed this post? Try these related posts

  1. fn:toUpperCase() – JSTL Function
  2. fn:length() – JSTL Function
  3. fn:replace() – JSTL Function
  4. JSTL <c:if> Core Tag
  5. fn:endsWith() – JSTL Function
  6. JSTL <c:forEach> and <c:forTokens> Core Tags

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()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap