fn:replace() JSTL Function

fn:replace() JSTL Function Example explains about replacing a specified input string with all occurrences of a existing string

Consider an example where you need to replace an existing string with some other value.

fn:replace() function returns a string that resulting from replacing the existing string with the given input string.

java.lang.String replace(java.lang.String,java.lang.String,java.lang.String)

You can see the below example, which is demonstrating fn:replace() JSTL Function

Required Libraries

You need to download

  1. Tomcat 9
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

fn:replace() Example

<%@ 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()</title>
</head>
<body>
    <%
    // you can also set the values into request scope same as using c:set
    // request.setAttribute("str", "abc string");
    %>

    <c:set var="str" value="abc string" />

    <c:set var="str" value="${fn:replace(str,'abc','abcd')}"/>
    ${(str)}

</body>
</html>
Output
abcd string

 











Your email address will not be published. Required fields are marked *