Use of <fn:split(String, String)> and <fn:join(String, String)> Tag of JSTL
In this section we will learn how to use <fn:split
>and
<fn:join> Tag of JSTL. These tags are used to split and join the specified
string according to the given delimeter.
<fn:split> It splits a string into an array of sub strings on the
basis of specified delimeter.
<fn:join> It joins all elements of an array into a string with the
specified separator.
Syntax : |
java.lang.String[] split(java.lang.String,
java.lang.String) |
split_fnJstlTag.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <html> <head> <title>Example fn:split and fn:join tag of JSTL</title> </head> <body> <c:set var="str1" value="This is first string"/> <c:set var="str2" value="This|is|second|string"/> <c:set var="str3" value="This|is+third*string"/> <h4>tag fn:split(Here we will split the given string in to array of string on the <br>basis of given delimeter)</h4> <table width="50%" border="1"> <tr> <th>Input String</th> <th>Delimiter(s)</th> <th>After split</th> </tr> <tr> <td>${str1}</td> <td>white space</td> <td> <c:forEach var="num" items="${fn:split(str1, ' ')}"> <c:out value="${num}" /> </c:forEach> </td> </tr> <tr> <td>${str2}</td> <td>|</td> <td> <c:forEach var="num" items="${fn:split(str2, '|')}"> <c:out value="${num}" /> </c:forEach> </td> </tr> <tr> <td>${str3}</td> <td>+|*</td> <td> <c:forEach var="num" items="${fn:split(str3, '+|*')}"> <c:out value="${num}" /> </c:forEach> </td> </tr </table> <h4>tag fn:split(Here we will join the splitted array of string in to a single string <br>with the given separator)</h4> <table cellpadding="5" border="1"> <tr> <th>Separator</th> <th>After join</th> </tr> <tr><c:set var="a1" value="${fn:split(str1, ' ')}" /> <td> white space</td> <td><c:out value="${fn:join(a1, ' ')}" /></td> </tr> <tr><c:set var="a1" value="${fn:split(str2, '|')}" /> <td>|</td> <td><c:out value="${fn:join(a1, ' | ')}" /></td> </tr> <tr><c:set var="a1" value="${fn:split(str3, '|+*')}" /> <td>***</td> <td><c:out value="${fn:join(a1, ' *** ')}" /></td> </tr> </table> </body> </html> |
This jsp code shows the following output :