Updated April 6, 2023
Introduction to JSTL replace() Function
JSTL replace() function is used to replace the existing string with new required string. This replace() function is case sensitive by default. Which is used as fn:replace() in the code. This replace() function is sub part of function tags. JSTL abbreviatedas Java Standard Tag Library. Which is further extension for JSP (Java Server Pages). JSTL reduced the lines of code for developer. This JSTL supports for structural tasks, common task like conditional and iteration. This tags used for changing I18N (Internationalization) tags, SQL tags, XML documents etc. This JSTL alsoprovides a framework for attaching the already existing custom tags within the Java Standard Tag Library.
Real Time Example: Suppose we have a string with some original content. Later we got to know some part of string has to be modified. It is very difficult to read line by line and replace the string manually. So overcome this we have a replace function to modify previous string with a new string.
Advantages:
- Easy to replace the string.
- A faster way to search and replace the string.
How Does replace() Function Work in JSTL?
JSTL replace() function works based on the existing string and new string to replace. This function only works if we use <%@ taglib uri=”http://java.sun.com/jsp/jstl/functions” prefix=”fn” %>uri, and if we use core tags then we have to use <%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>uri.
Syntax:
fn:replace(String input, String “searchable string”, String “replaced string”) ;
Examples to Implement of JSTL replace() Function
Below are the examples of JSTL replace:
Example #1 – Replace() function with Long Text
Code:
<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<title>replace function</title>
<style type="text/css">
h1 {
color: green;
text-align: center;
}
p {
color: blue;
border: 2px ridge red;
font-size: 20px;
}
</style>
</head>
<body>
<h1>Replace function Demo</h1>
<p>
<c:setvar="intro"
value="JSTL replace() function is used to replace the existing string with new required string. This replace() function is case sensitive by default. Which is used as fn:replace() in the code. This replace() function is sub part of function tags. JSTL abbreviated as Java Standard Tag Library. Which is further extension for JSP (Java Server Pages). JSTL reduced the lines of code for developer. This JSTL supports for structural tasks, common task like conditional and iteration. This tags used for changing I18N (Internationalization) tags, SQL tags, XML documents etc. This JSTL also provides a framework for attaching the already existing custom tags within the Java Standard Tag Library." />
<!-- every replace() string replaced by capital REPLACE()-->
${
fn:replace(intro, "replace()", "REPLACE()")
}
</p>
</body>
</html>
Output:
Example #2 – Replace names with other Names by using replace() Function
Code:
<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<title>replace function</title>
<style type="text/css">
h1 {
color: brown;
text-align: center;
}
p {
color: fuchsia;
border: 2px ridge green;
font-size: 20px;
}
</style>
</head>
<body>
<h1>Replace function Introduction</h1>
<p>JSTL replace() function is used to replace the existing string
with new required string. This replace() function is case sensitive by
default. Which is used as fn:replace() in the code. This replace()
function is sub part of function tags. JSTL abbreviated as Java
Standard Tag Library.</p>
<h1>Replacing initial name with other name by using replace()
function</h1>
<p>
<c:setvar="name" value="My best friend name is Amardeep" />
<!-- replace initial name with other name-->
${
fn:replace(name, "Amardeep", "Patel")
}
</p>
<p>
<c:setvar="course" value="I am very good at Java." />
<!-- replace initial name with other name-->
${
fn:replace(course, "Java", "Spring")
}
</p>
<p>
<c:setvar="name" value="I am Paramesh" />
<!-- replace initial name with other name-->
${
fn:replace(name, "Paramesh", "Venkatesh")
}
</p>
</body>
</html>
Output:
Example #3 – Replacing “–“ with empty space(“ “) by using replace() Function
Code:
<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ tagliburi="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<title>replace function</title>
<style type="text/css">
h1 {
color: red;
text-align: center;
}
p {
color: orange;
border: 2px dotted navy;
font-size: 20px;
}
</style>
</head>
<body>
<h1>Replace function Introduction</h1>
<p>JSTL replace() function is used to replace the existing string
with new required string. This replace() function is case sensitive by
default. Which is used as fn:replace() in the code. This replace()
function is sub part of function tags. JSTL abbreviated as Java
Standard Tag Library.</p>
<h1>Replacing - name with empty space by using replace()
function</h1>
<p>
<c:setvar="iphon"
value="JSTL-abbreviated-as-Java-Standard-Tag-Library.-Which-is-further-extension-for-JSP (Java Server Pages). JSTL reduced the lines of code for developer.
This JSTL supports for structural tasks, common-task-like-conditional-and-iteration.-This tags-used-for-changing-I18N-(Internationalization)-tags, SQL tags,
XML documents etc. This JSTL also provides a framework-for-attaching-the-already-existing custom tags within the Java Standard Tag Library." />
<!-- replace - name with empty space " " name-->
${
fn:replace(iphon, "-", " ")
}
</p>
</body>
</html>
Output:
Conclusion
JSTL replace function is used to replace the initial sequence of character with user required sequence of character. Make sure that it is case sensitive like A and a are different.
Recommended Articles
This is a guide to JSTL replace. Here we discuss a brief overview on JSTL replace and its advantages along with examples and Code Implementation. You can also go through our other suggested articles to learn more –