Open In App

JSTL fn:substring() Function

In JSTL, the fn:substring() function is used to extract or retrieve the part of a given input or specified string, by starting from the start index to the end index [optional]. This function is mainly used for string manipulation tasks by many developers.

In this article we will explore the Syntax along with the Parameters of the JSTL fn:substring() function, also we will see the practical implementation of this function in terms of examples.



Syntax of JSTL fn:substring() Function

${fn:substring(string, startIndex ,endIndex)}

Where,

Note: If this index is not included, then the substring continues to the end of the input string



Example of fn:substring()

In this example, we will discuss the use of fn:substring(), to generate different substrings.




<%@ 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:substring() Example</title>
  </head>
  <body>
    <c:set var="mainString" value="GeeksforGeeks"/>
    <h3>Main String: ${mainString}</h3>
      
    <p>Substring from index 5 to the end: ${fn:substring(mainString, 5)}</p>
    <p>Substring from index 0 to 5: ${fn:substring(mainString, 0, 5)}</p>
   
  </body>
</html>

Output:

Output

Explanation of the above Program:

Article Tags :