This post helps you understand how to use the <fn:replace> tag in the JSTL function tags library with code example.

The fn:replace function replaces all the occurrences of a given string with new string and returns resulting string.

 

JSTL <fn:replace> Syntax:

${fn:replace(<text>, <string to be replaced>, <string to replace>)}

 

JSTL <fn:replace> Example:

The following JSP code replaces string representation of number to number in the given string.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>fn:replace Demo</title>
  </head>
  <body>
	<h1>fn:replace Demo</h1>

	<c:set var="text" value="There are sixteen JSTL functions." />
	Before replace:  <c:out value="${text}"/>
	<c:set var="text" value="${fn:replace(text,'sixteen', '16')}" /><br/>
	After replace:  <c:out value="${text}"/>
		
  </body>
</html>

 

Output:

JSTL function fn-replace

 

Other JSTL Function Tags:

contains  |  containsIgnoreCase  |  endsWith |  escapeXml  |  indexOf  |  join  |  length  |  split  |  startsWith  |  substring  |  substringAfter  |  substringBefore  |  toLowercase  |  toUpperCase  |  trim


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.

Add comment