replaceAll() multiples- looking for elegant solution
I've looked and haven't seen a thread regarding my specific requirement for replaceAll(); but I hope this isn't a re-post. Anyway, I'm cleaning up some data and am looking for an elegant way to replace multiple characters in a String with others.
Characters: (&, ", <, >)
Here's my current ugly way:
String foo = bar.replaceAll("&", "&").replaceAll("\", """).replaceAll("<", "<").replaceAll(">", ">");
I know replaceAll(); can be used to replace multiple chars with a single char, but I want to set up an if this, then this, if this, then this-- akin to Oracle SQL DECODE function that does:
DECODE(foo, (if_this), (then_this), (if_this), (then_this), (if_this), (then_this), (else_this))
I'm a novice so bear with me. I've researched to the best of my ability and don't have any leads. Constructive input is appreciated!
Re: replaceAll() multiples- looking for elegant solution
Quote:
Originally Posted by
Redefine12
Here's my current ugly way:
String foo = bar.replaceAll("&", "&").replaceAll("\", """).replaceAll("<", "<").replaceAll(">", ">");
What is so ugly about it? Another solution requires a StringBuilder but it takes a couple of loops; I think your solution is one of the shortest solutions.
kind regards,
Jos
Re: replaceAll() multiples- looking for elegant solution
Well, the only other thing I could suggest is to create a method that will do this for you. Your example looks fine to me, but maybe this is what you are looking for:
Code:
public class replacer {
public static String replaceWithCharacters(String alter, String[] org, String[] repl) {
for(int i = 0; i < org.length; i++) {
alter = alter.replaceAll(org[i], repl[i]);
}
return alter;
}
public static void main(String args[]) {
String[] Orginal = { "&", "\"", "<", ">" };
String[] Replace = { "&", """, "<", ">" };
String bar = " & the cow\"s Go <MOOOOOOO>";
//String foo = bar.replaceAll("&", "&").replaceAll("\"", """).replaceAll("<", "<").replaceAll(">", ">");
String foo = replaceWithCharacters(bar, Orginal, Replace);
System.out.println(foo);
}
}
Re: replaceAll() multiples- looking for elegant solution
Thank you both! I like the OOP approach of your discrete method, kammce.
Josah, funny side-note: when you quote my String foo = bar.replaceAll... the website shows it as
String foo = bar.replaceAll("&", "&").replaceAll("\", """).replaceAll("<", "<").replaceAll(">", ">");
Thanks again :)
Re: replaceAll() multiples- looking for elegant solution
Wait! one last thing! That approach is really, really, SIMPLE!!! Make sure to create something inside of the replaceWithCharacters() function that will check to see if the original and the replace arrays are of the same length and put a try catch statement around for statement for good measure. This will catch an error and not crash, in the event that your array length tests fail for some reason. Or something like that, because, you never know, you might run across some issues if you do not put these precautions in there.
Happy programming!
Re: replaceAll() multiples- looking for elegant solution
Quote:
Originally Posted by
kammce
Wait! one last thing! That approach is really, really, SIMPLE!!! Make sure to create something inside of the replaceWithCharacters() function that will check to see if the original and the replace arrays are of the same length and put a try catch statement around for statement for good measure. This will catch an error and not crash, in the event that your array length tests fail for some reason. Or something like that, because, you never know, you might run across some issues if you do not put these precautions in there.
And so the entire thing became uglier and uglier ... what is wrong with the original version? For a small/fixed set of characters, a functional notation is as short and efficient as possible; there is no need to explicitly loop over that character set or anticipate for special rules or whatever.
kind regards,
Jos