Multiple string replacements without affecting substituted text in subsequent iterations
<p>I've posted about letters earlier, but this is an another topic, I have a json response that contain 2 objects, <code>from</code> and <code>to</code> , <code>from</code> is what to change, and <code>to</code> is what it will be changed to .</p>

<p>My code is : </p>

<pre><code>// for example, the EnteredText is "ab b test a b" .
EnteredString = EnteredText.getText().toString();
for (int i = 0; i &lt; m_jArry.length(); i++) {
    JSONObject jo_inside = m_jArry.getJSONObject(i);

    String Original = jo_inside.getString("from");
    String To = jo_inside.getString("to");

    if(isMethodConvertingIn){
        EnteredString = EnteredString.replace(" ","_");
        EnteredString = EnteredString.replace(Original,To + " ");
    } else {
        EnteredString = EnteredString.replace("_"," ");
        EnteredString = EnteredString.replace(To + " ", Original);
    }
}

LoadingProgress.setVisibility(View.GONE);
SetResultText(EnteredString);
ShowResultCardView();
</code></pre>

<p>For example, the json response is : </p>

<pre><code>{
    "Response":[
        {"from":"a","to":"bhduh"},{"from":"b","to":"eieja"},{"from":"tes","to":"neesj"}
    ]
}
</code></pre>

<p><code>String.replace()</code> method won't work here, because first it will replace <code>a</code> to <code>bhduh</code>, then <code>b</code> to <code>eieja</code>, BUT here's the problem, it will convert <code>b</code> in <code>bhduh</code> to <code>eieja</code>, which i don't want to. </p>

<p>I want to perfectly convert the letters and "words" in the String according the Json, but that what i'm failing at .</p>

<p>New Code : </p>

<pre><code>if(m_jArry.length() &gt; 0){
    HashMap&lt;String, String&gt; m_li;

    EnteredString = EnteredText.getText().toString();

    Log.i("TestAf_","Before Converting: "  + EnteredString);

    HashMap&lt;String,String&gt; replacements = new HashMap&lt;String,String&gt;();
    for (int i = 0; i &lt; m_jArry.length(); i++) {
        JSONObject jo_inside = m_jArry.getJSONObject(i);

        String Original = jo_inside.getString("from");
        String To = jo_inside.getString("to");

        if(isMethodConvertingIn){

            //EnteredString = EnteredString.replace(" ","_");

            replacements.put(Original,To);
            Log.i("TestAf_","From: " + Original + " - To: " + To + " - Loop: " + i);
            //EnteredString = EnteredString.replace(" ","_");
            //EnteredString = EnteredString.replace(Original,To + " ");

        } else {

            EnteredString = EnteredString.replace("_"," ");
            EnteredString = EnteredString.replace("'" + To + "'", Original);
        }

    }
    Log.i("TestAf_","After Converting: " + replaceTokens(EnteredString,replacements));

    // Replace Logic Here
    // When Finish, Do :
    LoadingProgress.setVisibility(View.GONE);
    SetResultText(replaceTokens(EnteredString,replacements));
    ShowResultCardView();
</code></pre>

<p>Output : </p>

<pre><code>10-10 19:51:19.757 12113-12113/? I/TestAf_: Before Converting: ab a ba
10-10 19:51:19.757 12113-12113/? I/TestAf_: From: a - To: bhduh - Loop: 0
10-10 19:51:19.757 12113-12113/? I/TestAf_: From: b - To: eieja - Loop: 1
10-10 19:51:19.757 12113-12113/? I/TestAf_: From: o - To: neesj - Loop: 2
10-10 19:51:19.758 12113-12113/? I/TestAf_: After Converting: ab a ba
</code></pre>