Search code examples
javastringtextreplaceall

Doing String.replaceAll multiple times ignoring previous letters replaced Java


I have a problem that now has come down to having two arraylists with the letters of the alphabet in them in some order. I want to replace all the letters in a string with the ones in the first arrraylist with the one in the second (i.e the element at index 1 of arraylist 1 to be replaced with element 1 in arraylist 2 in the string).

if we have arraylist 1 being [a,b,c,d] and arraylist 2 being [b,a,d,c]. and a string that is made up of these letters, I want to replace a with b, b,with a ...etc However when i replace a with b, the next replacement is b with a, so all of the b's and a's are changed to a's. I can't think of a way to avoid this problem. (My problem is using all of the letters of the alphabet and a huge text file though.)

Edit:

I read that back and i explained it appallingly. Here is some code to help explain. Lets say we have ArrayList1 = [r, g, n, b, v, a, u, f, e, q, y, h, z, j, p, l, s, t, c, o, i, x, w, k, d, m]

ArrayList2 = [e, t, a, o, i, n, s, r, h, d, l, u, c, m, f, y, w, g, p, b, v, k, x, q, j, z]

lets say we also have a string we want to decode using those 2 arraylists in the manner i talked about above:

String s = "Gb Fureybpx Ubyzrf fur vf nyjnlf gur jbzna. V unir fryqbz 
    urneq uvz zragvba ure haqre nal bgure anzr. Va uvf rlrf fur rpyvcfrf 
    naq cerqbzvangrf gur jubyr bs ure frk.";

    for(int i =0;i < ArrayList.size() ; i++){
    s = s.replaceAll(ArrayList1.get(i),ArrayList2.get(i);
    }

This won't work properly because r -> e and then later on e -> h and so on. This is what i am trying to avoid but am struggling of thinking of a way to do so.


Solution

  • There isn't a good way to use regex like you want to

    If your input is restricted to all lowercase/uppercase letters, you could do some tricks with that

    String encode(String in, ArrayList<String> a, ArrayList<String> b) //in is lowercase
    {
        in = in.toUpperCase();
        for(int i = 0; i < a.size(); i++)
            in = in.replaceAll(a.get(i).toUpperCase(), b.get(i));
        return in;
    }
    

    If you're dealing with the full alphabet, it may make more sense to just use array indices

    The replacements string will encode "abcd...wxyz" to it

    String encode(String in, String replacements)
    {
        StringBuilder out = new StringBuilder();
        for(char c : in.toCharArray())
            out.append(replacements.charAt(c - 'a')); //can swap replacements with an array/ArrayList: replacements[c - 'a'] and replacements.get(c - 'a'a)
        return out.toString();
    }
    

    encode("test", "abcdefghijklmnopqrstuvwxyz") = test
    
    encode("test", "bcdefghijklmnopqrstuvwxyza") = uftu
    

    If you must use ArrayList instead :

    String encode(String in, ArrayList<String> a, ArrayList<String> b)
    {
        StringBuilder out = new StringBuilder();
        for(char c : in.toCharArray())
            out.append(b.get(a.indexOf("" + c))); //should do error checking if the input contains characters not in a
        return out.toString();
    }