0

I come from a PHP background. I just want to do a simple string replace.

I just want to replace any question marker with other character of '-'. in that "Material" string.

If I do

<%= Material %>

it will simple like write out "BBBB??AC".

I have no another access but just one .jsp file.

Is there another easy way i can do a string replace and print it out?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Bill
  • 17,872
  • 19
  • 83
  • 131

2 Answers2

4

Assuming this Material in your scriptlet is a String, then just do

<%= Material.replaceAll("\\?", "-") %>

This would basically solve your doubt.

Still, if you can, stop using scriptlets, the reasons are better explained here: How to avoid Java code in JSP files?

Based on the posted link, the solution would be using EL and JSTL functions:

${fn:replace(Material, '?', '-')}

From your comment, since Material is not a String, you could perform a call to toString method before applying the replace. In scriptlet:

<%= Material.toString().replaceAll("\\?", "-") %>

In EL/JSTL you will need a temp variable to handle this:

<c:set var="materialString">
     ${Material}
</c:set>
${fn:replace(materialString, '?', '-')}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Hi Luiggi, this is great, but the application just stop render if i use replace method. So i think Material possible is not a String. Is there a way i can parse_string in JSP? – Bill May 30 '13 at 03:09
  • 1
    If `Material` is not a String, probably you would use `Material.toString()`. – Luiggi Mendoza May 30 '13 at 03:09
  • Sweet Lugiggi Thanks so much for your kind help – Bill May 30 '13 at 03:19
  • Quick one do i need to important any Class in order to do this replace method? – Bill May 30 '13 at 03:23
  • 1
    @bluebill1049 since is a `String`, no. If you're going to use JSTL, you will have to. Please refer to [StackOverflow JSTL wiki](http://stackoverflow.com/tags/jstl/info) for more info about it. – Luiggi Mendoza May 30 '13 at 03:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30877/discussion-between-bluebill1049-and-luiggi-mendoza) – Bill May 30 '13 at 04:48
1

You can replace string with <%= Material.replaceAll("\\?","=");%>

Parth Soni
  • 11,158
  • 4
  • 32
  • 54