Open In App

JSTL fn:split() Function

The JSTL fn:split() function is used to split the input string into an array of different substrings based on the delimiter. This function is helpful to the parsing of strings within the JavaServer Pages, which also allows the developers to extract the substring from the main string.

In this article, we will see the syntax along with the parameters of the fn:split() function. We will also explore the practical implementation of this function in terms of examples.



Syntax of fn:split() Function

<c:set var="arrayVariable" value="${fn:split(inputString, delimiter)}"/>

Where,

Example of fn:split() Function

Complete working example discussing the use case of fn:split()




<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ 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="inputString" value="Geeks for Geeks"/>
      
    <c:set var="splitArray" value="${fn:split(inputString, ' ')}"/>
      
    <h3>Original String: ${inputString}</h3>
    <p>Split Array:</p>
    <ul>
        <c:forEach var="substring" items="${splitArray}">
            <li>${substring}</li>
        </c:forEach>
    </ul>
  </body>
</html>

Output:

Original String: Geeks for Geeks
Split Array:
-Geeks
-for
-Geeks

Output Screen of the fn:split() Function Program:



Explanation of the above Program:


Article Tags :