[Logo]
Forums Register Login
JSP JSTL String Replace all ignoring case
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.
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?
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.
 
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.


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.
Your best bet is to fix the inconsistencies, which should be pretty simple.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 21, 2017 12:53:05.