Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: replaceAll() multiples- looking for elegant solution

  1. #1
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default 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("&", "&amp").replaceAll("\", "&quot").replaceAll("<", "&lt").replaceAll(">", "&gt");

    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!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: replaceAll() multiples- looking for elegant solution

    Quote Originally Posted by Redefine12 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default 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 = { "&amp", "&quot", "&lt", "&gt" };
    		String bar = " & the cow\"s Go <MOOOOOOO>";
    		//String foo = bar.replaceAll("&", "&amp").replaceAll("\"", "&quot").replaceAll("<", "&lt").replaceAll(">", "&gt");
    		String foo = replaceWithCharacters(bar, Orginal, Replace);
    		System.out.println(foo);
    	}
    }
    My API:
    Java Code:
    cat > a.out || cat > main.class

  4. #4
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default 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 :)

  5. #5
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default 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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: replaceAll() multiples- looking for elegant solution

    Quote Originally Posted by kammce View Post
    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
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. 2d array - elegant population?
    By Redefine12 in forum New To Java
    Replies: 17
    Last Post: 03-19-2012, 10:47 PM
  2. Print Odd Multiples of 3
    By mp3_1 in forum New To Java
    Replies: 9
    Last Post: 11-22-2011, 09:09 PM
  3. Problem with replaceAll
    By Timmy in forum New To Java
    Replies: 6
    Last Post: 06-06-2011, 04:51 PM
  4. Multiples Integers from a String
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 04-01-2009, 12:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •