beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

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

Enjoyed this post? Try these related posts

  1. fn:length() – JSTL Function
  2. JSTL <c:forEach> and <c:forTokens> Core Tags
  3. JSTL <c:catch> Core Tag
  4. JSTL <c:set> Core Tag
  5. JSTL <c:redirect> Core Tag
  6. fn:endsWith() – JSTL Function

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()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap