The JSTL Function <fn:split()> is the string function. This function is used to split or separate the string with specified delimiter(separator). JSTL Function <fn:split()> helps to split or separate a specified string into an array of substring.
Syntax of JSTL Function <fn:split()>
java.lang.String[] split(java.lang.String, java.lang.String)
Example of JSTL Function <fn:split()>
<%@ 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>Example of Function fn:split()</h1> <c:set var="name" value="welcome-to-JavaBeat" /> <c:set var="splitName" value="${fn:split(name,'-')}" /> <c:set var="joinedName" value="${fn:join(splitName,' ')}" /> <p>Name before split: <br><br><b>${name}</b></p> <p>Name after split: <br><br><b>${joinedName}</b></p> </body> </html>
Details of the Code
- <c:set var=”name”> tag is used to set the variable name which we want to display in the output.
- < c:set var=”splitName” value=”${fn:split(name,’-‘)}”> this line of code used to display the name separated by a specified separator.
- Name before split: ${name} this line of code displays the name before it split.
- after split:joinedName} this line of code displays the name after splitting.
Steps for Execution
- Click on the jsp File
- Right click on mouse and select run as ->click run on server
- The simple way for execute the program on eclipse is to use Ctrl+F11 key then click on select your browser option and click on finish
Output
When the execution process completed successfully we will get the output on eclipse browse as follows:
Previous Tutorial : JSTL Function fn:join() :: Next Tutorial : JSTL Function fn:startsWith()