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

The fn:split function splits a string into an array of substring based on the delimiter provided. This function returns array of strings.

 

JSTL <fn:split> Syntax:

${fn:split(<string with delimiters>, <delimiter>)}

 

JSTL <fn:split> Example:

The below code example splits a string with comma (,) as a delimiter. Note that since this function returns an array of strings, we need to use fn:join function to join the individual strings in the array to display.

<%@ 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:split Demo</title>
  </head>
  <body>
	<h1>fn:split 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 split: ${numbers}</p> <p>Numbers after split and join: ${joinedNumbers}</p> </body> </html>

 

Output:

JSTL function fn-split

 

Other JSTL Function Tags:

contains  |  containsIgnoreCase  |  endsWith |  escapeXml  |  indexOf  |  join  |  length  |  replace  |  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

   


Comments 

#1Hiren2015-12-24 06:37
I Want to split character by character like String is Hiren I want split like this {'H','i','r','e','n'} using jstl split function
Quote