Open In App

JSTL fn:split() Function

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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,

  • inputString: This is the input or main string which be split into different substrings.
  • delimiter: This is the delimiter used to split the string into an array of substrings.
  • arrayVariable: This is the variable in which the array of substrings will be stored.

Example of fn:split() Function

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

HTML




<%@ 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:

Output Screen of the fn:split() Function Program

Explanation of the above Program:

  • Initialise the inputString variable with the string as “Geeks for Geeks“.
  • <c:set> tag which uses the fn:split() function to split the string which is based on the space delimiter.
  • final substrings are stored in the splitArray variable.
  • We are then showing the substrings in an unordered using the <ul> tag in HTML.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads