Open In App

JSTL fn:replace() Function

The JSTL fn:replace() function or method in JSP (Java Server Pages) is mainly used for the manipulation of strings. This function allows developers to replace the occurrence of a specified substring with another substring in the given input string. This article will see the Syntax, parameters, and two practical examples of the fn:replace() function. We need to note that the replace function performs case-sensitive processing of string.

Syntax of fn:replace()

${fn:replace(sourceString, targetSubstring, replacementSubstring)}

Where,



Example of fn:replace() function

Below is the implementation of the fn:replace() function:




<%@ 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:replace() Example</title>
</head>
<body>
<c:set var="originalString" value="GeeksforGeeks is a platform for computer science students. 
                                   GeeksforGeeks is good." />
<p>Original String: ${originalString}</p>
<c:set var="modifiedString" value="${fn:replace(originalString, 'GeeksforGeeks', 'GFG')}"/>
<p>Modified String: ${modifiedString}</p>
</body>
</html>

Output:

Original String: GeeksforGeeks is a platform for computer science students. GeeksforGeeks is good.
Modified String: GFG is a platform for computer science students. GFG is good.

Final Output Shown in the Browser:



Explanation of the above Program:

Article Tags :