0

For example I want to replace , from value A,B. .C with a blank

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <c:set var="pat" value="A,B.  .C" /> 
        <c:out value="${pat}"/></br >
            ${fn:replace(pat, ",", " ")}
        <c:out value="${pat}"/></br >
    </body>
</html>

expected output :

A,B. .C
A B. .C

Real output :

A,B. .C
A B. .C A,B. .C

Browser Picture

enter image description here

After the replaced value contains the variable ${pat} also the original value.

What do I wrong ? Never see this before.
with these results, the function is useless.

(tested with jdk1.6.0_45 and jdk1.7.0_51 Java EE Version : Java EE 6 Web)

EDIT

from answer @rickz

I tested with

<c:set var="pat" value="A,B.  .C" /> 
   <c:out value="${pat}" /></br >
   ------------------------------</br >
   ${fn:replace(pat, ",", " ")}</br >
   ------------------------------</br >    
   <c:out value="${pat}" /></br >
   ------------------------------</br >

And the output :

A,B. .C
------------------------------
A B. .C
------------------------------
A,B. .C
------------------------------

We can see the value of ${pat} is not changed
Most of the manuals on the web did not count on that ( or not explain).

example 1
What is the simplest way to replace quote characters with \" sequence inside string values?

and many others

Community
  • 1
  • 1
moskito-x
  • 11,832
  • 5
  • 47
  • 60

1 Answers1

1

Please try it this way.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <c:set var="pat" value="A,B.  .C" /> 
    ${pat}</br >
    ${fn:replace(pat, ",", " ")}
</body>
</html>
rickz
  • 4,324
  • 2
  • 19
  • 30