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.
Leave a Reply