BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

fn:replace() – JSTL Function

By Chaitanya Singh | Filed Under: JSTL

It search for a string in the input and replace it with the provided string. The following is the basic syntax of fn:replace() function.

Syntax

String fn:replace(String input, String search_for, String replace_with)

Three string arguments and return type is also String. It searches the search_for string in input and replaces it with replace_with string. If the string is not found it returns the actual input as it is.

Note: It does case sensitive processing.

Example

In this example we are using fn:replace() function on two input strings.

<%@ 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="author" value="Chaitanya Singh"/>
<c:set var="randomstring" value="abc def abc ghi ABC"/>
${fn:replace(author, "Chaitanya", "Rahul")} 
${fn:replace(randomstring, "abc", "hello")}
</body>
</html>

Output:

Observe the output you would find that the “ABC” in randomstring variable remains as it is while the other “abc” substrings got replaced with “hello”. It happened because of the case sensitive match, we are replacing lowercase “abc” in the above example.

fn-replace-example

Leave a Reply Cancel reply

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

JSTL Core Tags

  • c:out
  • c:set
  • c:remove
  • c:if
  • c:choose
  • c:when
  • c:otherwise
  • c:catch
  • c:import
  • c:forEach
  • c:forTokens
  • c:param
  • c:url
  • c:redirect
JSTL Functions
  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:indexOf()
  • fn:escapeXML()
  • fn:join() & fn:split()
  • fn:length()
  • fn:startsWith()
  • fn:endsWith()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:trim()
  • fn:toUpperCase()
  • fn:toLowerCase()
  • fn:replace()

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap