JSTL Function fn:split Example
- Details
- Written by Nam Ha Minh
- Last Updated on 30 August 2019   |   Print Email
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:
Other JSTL Function Tags:
contains | containsIgnoreCase | endsWith | escapeXml | indexOf | join | length | replace | startsWith | substring | substringAfter | substringBefore | toLowerCase | toUpperCase | trim
Comments