Results 1 to 6 of 6
- 07-20-2012, 07:52 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
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!
- 07-20-2012, 08:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: replaceAll() multiples- looking for elegant solution
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-20-2012, 09:11 PM #3
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:
Java 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); } }My API:Java Code:cat > a.out || cat > main.class
- 07-20-2012, 09:30 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
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 :)
- 07-20-2012, 09:48 PM #5
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!My API:Java Code:cat > a.out || cat > main.class
- 07-21-2012, 08:14 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: replaceAll() multiples- looking for elegant solution
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
2d array - elegant population?
By Redefine12 in forum New To JavaReplies: 17Last Post: 03-19-2012, 10:47 PM -
Print Odd Multiples of 3
By mp3_1 in forum New To JavaReplies: 9Last Post: 11-22-2011, 09:09 PM -
Problem with replaceAll
By Timmy in forum New To JavaReplies: 6Last Post: 06-06-2011, 04:51 PM -
Multiples Integers from a String
By AndrewM16921 in forum New To JavaReplies: 3Last Post: 04-01-2009, 12:00 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks