-2

I have two Strings and a Json response :

  1. InputString = "ab";
  2. OutputString = "";
  3. Json(example, not real one): Array(From:a,to:bhduh - From:b, to:eiaja).

*Json isn't a real response, it has 2 records for this example .

What i want to do is to replace a with bhduh and b with eiaja, I have a JSON loop which tell me to what i should replace, and i need to do it inside that loop, So here's what i tried :

InputString = InputString.replace(From,To); 

Output

eiajahduheiaja

Expected Output

bhduheiaja

This's happening because in the first loop, it's changing a, and in the second loop, there's two b, the b in bhduh and b in the normal String.

The loop times depend on letters count, so sometimes it can be 5 or 6, depending on the server Json response.

What i want is to have the Expected Output, any ideas ?

Jaeger
  • 1,646
  • 8
  • 27
  • 59
  • Downvoter, can you explain why ? – Jaeger Oct 10 '16 at 16:23
  • Json is a String? I don't understand how your Json (object?) is. Is something like "{ array: [ "from:a,to:bhduh",from:b,to:eiaja" ]}" ? – Wallkan Oct 10 '16 at 16:28
  • 1) #3 Isn't JSON. 2) What is a "JSON loop'? – OneCricketeer Oct 10 '16 at 16:28
  • If you hover over down-vote arrow you will see tooltip which explains when this arrow should be used. In your case I suspect that down-vote was caused by question being *unclear* (I am guessing that json which isn't json could have something to do with it). – Pshemo Oct 10 '16 at 16:30
  • If I understand correctly, this answer to your previous question can be adapted to your current problem. http://stackoverflow.com/a/39950173/2308683 – OneCricketeer Oct 10 '16 at 16:31
  • For json i was lazy to put a real Json response and Json parser ..etc so i just made a shortcut for it. – Jaeger Oct 10 '16 at 16:34
  • I would probably use Matcher and its `appendReplacement` and `appendTail` like shown here: http://stackoverflow.com/a/21419100/1393766. But to provide proper answer I would need to know that I can assume things like if you really want to replace only single characters, what is the range of characters (like is `-` possible or not). – Pshemo Oct 10 '16 at 16:34

2 Answers2

1

It is unclear what JSON you have, but I'm guessing your problem is that you are replacing a with bhduh, (which contains a b), then you try to replace b with some other stuff. Obviously that isn't correct.

You need to loop over your initial input, then append your replacements to some other string, not replace upon your input.

For example, using a HashMap

public static void main (String[] args) throws java.lang.Exception
{
    String input = "ab";
    HashMap<Character, String> replacements = new HashMap<Character, String>() {{
        put('a', "bhduh");
        put('b', "eiaja");
    }};

    StringBuilder sb = new StringBuilder();
    for (char c : input.toCharArray()) {
        String rep = replacements.get(c);
        if (rep != null) {
            sb.append(rep);
        }
    }
    System.out.println(sb.toString());
}

Example

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • You're correct, Json isn't important in my issue, my issue is about mis-replacing letters. Also, Json contain Strings, as it may be FROM `example` to `dS$@_` , not just `a` => `bhduh`, and your example only take chars. – Jaeger Oct 10 '16 at 16:43
  • Again, you should have made that more clear in your question. You only have `a` and `b` as your possible characters to replace. Obviously, replacing whole strings is a more complicated problem – OneCricketeer Oct 10 '16 at 16:46
  • Your code is what i need, thanks a lot. beside, any tip for making for String ? – Jaeger Oct 10 '16 at 16:48
  • 1
    My answer could have easily been `HashMap`, then `"a"` instead of `'a'`, but then I would have needed to `replacements.get(""+c)` to convert the character to a string. You are welcome to post a [new question](http://stackoverflow.com/questions/ask) with the more involved string replacement, and I may see it – OneCricketeer Oct 10 '16 at 16:50
  • I've posted it here, give it a look if you want : http://stackoverflow.com/questions/39963316/replacing-string-letters-and-words-in-complex-way , thanks in advance. – Jaeger Oct 10 '16 at 17:06
0

If you replace all the original a's with some special character like - and all the original b's with another special character like * you should then be able to replace the special characters with the desired phrase.

Something like:

String str = "ab";
str =str.replace("a", "-");
str =str.replace("b", "*");
str =str.replace("-", "bhduh");
str =str.replace("*","eiaja");

System.out.println(str);

very likely not the best solution but possibly a working one for now.

Chains
  • 456
  • 4
  • 14