• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP JSTL String Replace all ignoring case  RSS feed

 
David Cowan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I have hit one of those small speed bumps in my code, so could do with a hand please. I am trying to do within a JSP using JSTL a string replace but the string compare must ignore case. For instance:

Sample input string "Hi ${Firstname}"

I want to do a replace and replace Firstname with John. But the curve ball is I am dealing with a lot of legacy records and the capitalisation of the string inside the ${...} varies greatly. So is sometimes ${firstname} and even ${firstName}. Sloppy I know but it is someone else's legacy I have to live with.

Anyway, it appears to me that the fn:replace function is only ever case sensitive with no override to make it case insensitive, is this correct?

All ideas welcome, but if possible would like to do it in a JSP.

Thanks in advance - Meanwhile I will go back to scratching my head on this.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait a minute. You are saying that there are typos in the scoped variable names that you are trying to fix with an EL function? Are you serious?

You don't fix typos in code with more code. You fix the code.

Or has your issue been misunderstood?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Backing away from the example: no, there is no case-insensitive variant of the JSTL's replace function. It's pretty easy to write your own though.
 
David Cowan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Wait a minute. You are saying that there are typos in the scoped variable names that you are trying to fix with an EL function? Are you serious?

You don't fix typos in code with more code. You fix the code.

Or has your issue been misunderstood?


Sorry if it wasn't clear. They aren't scoped variables, they are just string placeholders in a string, indicating they need to be replaced with an actual value at run time.

example

Input string - "Hi ${Firstname}, thanks for your comments."

should be after replacing ${Firstname} with run time value of John.

new string - "Hi John, thanks for your comments."

It is an unfortunate choice of string replacement notation I agree with respect to confusing things with JSTL. Hope this makes more sense.

I know in Java we could use string.replaceAll, I am just very surprised that they have left out of JSTL the equivalent given how many people must actually need to do it.


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, whew! Now I understand!

Yeah, I can see your point. On the other hand, I've been using JSTL since day 1 and never missed this functionality. They needed to draw the line somewhere, and a case-sensitive replace didn't make the cut. Of course you can "fake" one with a combination of replace() an toLowerCase(), or write your own EL function.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your best bet is to fix the inconsistencies, which should be pretty simple.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!