Replace a String with another using "fn:replace" tag of JSTL
In this Section, we will replace a String with another string using "fn:replace" tag of JSTL
In this Section, we will replace a String with another string using "fn:replace" tag of JSTL
Replace a String with another using <fn:replace> tag of JSTL
In this Section, we will replace a String with another string using <fn:replace> tag
of JSTL. This tag returns a string after replacing the given sub string with
specified sub string. This takes string type as arguments and returns string
type.
EXAMPLE:
In this Example, the string inputted will be replaced with other string/substring using
the <fn:replace> tag of JSTL. This takes string type as arguments and returns
string type.
fnReplaceJSTL.jsp
|
<%@
taglib
prefix="c"
uri="http://java.sun.com/jsp/jstl/core"
%>
<%@
taglib
prefix="fn"
uri="http://java.sun.com/jsp/jstl/functions"
%>
<html>
<head>
<title>Example
of fn:replace tag of Jstl tag library</title>
</head>
<body>
<form
method="POST">
<table>
<tr>
<td>Enter
String in you want to replace</td>
<td><input
type="text"
name="string"></td>
</tr>
<tr>
<td>Replace
character/string</td>
<td><input
type="text"
name="find"></td>
</tr>
<tr>
<td>Replace
with</td>
<td><input
type="text"
name="replaceWith"
size="3"></td>
</tr>
<tr> 0
<td></td>
<td><input
type="submit"
value="Replace
all"></td>
</tr> 1
</table>
</form>
<c:if
test="${pageContext.request.method=='POST'}"> 2
<c:set
var="string"
value="${param.string}"/>
<c:set
var="find"
value="${param.find}"/>
<c:set
var="replaceWith"
value="${param.replaceWith}"/> 3
<font
size="5"
color="#1B2A0A">
Result :
</font> 4
<font
size="5"
color="green">
<c:out
value="${fn:replace(string,find,replaceWith)}"/> 5
</font>
</c:if> 6
</body>
</ html>
|
Output :
7

After pressing button "Replace all" ,the output will be....

8
Download Source Code