Home Tutorial Java Jsp Replace a String with another using "fn:replace" tag of JSTL

 
 

Replace a String with another using "fn:replace" tag of JSTL
Posted on: July 2, 2010 at 12:00 AM
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>

<td></td>

<td><input type="submit" value="Replace all"></td>

</tr>

</table>

</form>

<c:if test="${pageContext.request.method=='POST'}">

<c:set var="string" value="${param.string}"/>

<c:set var="find" value="${param.find}"/>

<c:set var="replaceWith" value="${param.replaceWith}"/>

<font size="5" color="#1B2A0A">

Result :

</font>

<font size="5" color="green">

<c:out value="${fn:replace(string,find,replaceWith)}"/>

</font>

</c:if>

</body>

</html>

Output :

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

Download Source Code

Related Tags for Replace a String with another using "fn:replace" tag of JSTL:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.