-2

in this below String arrays i have this tags:

String[] tags = {
        "<mft:A>",
        "<mft:S>",
        "<mft:R>",
        "</mft:A>",
        "</mft:S>",
        "</mft:R>"
};

and i want to replace them with this html tags:

String[] replacementHtmlTags = {
        "<font color=\"red\">",
        "<font color=\"green\">",
        "<font color=\"blue\">",
        "</font>",
        "</font>",
        "</font>"
};

now after define targets and replacements my code doesn't work:

String rawParagraph = "11111 <mft:A>22222</mft:A> 33333 <mft:S> 44444 <mft:A> 555555 <mft:S> 66666 </mft:S></mft:A></mft:S><mft:R> 77777 </mft:R>"

for (int tag = 0; tag < tags.length; tag++) {
    rawParagraph.replace(tags[tag], replacementHtmlTags[tag]);
}
3
  • 1
    Strings are immutable, so replace() returns the new value. It doesn't (can't) update the current value. Commented Mar 5, 2017 at 6:26
  • @Andreas ok, so how can i replace all tags using with array like with my code? Commented Mar 5, 2017 at 6:31
  • @Andreas, thanks, problem resolved Commented Mar 5, 2017 at 6:36

1 Answer 1

1

I think you should follow this approach for better usability and mapping:

public static HashMap<String, String> keyVal;

static {
    keyVal = new HashMap<String, String>();
    keyVal.put("<mft:A>", "<font color=\\red\">");
    keyVal.put("<mft:S>", "<font color=\\green\">");
    keyVal.put("<mft:R>", "<font color=\\blue\">");
    keyVal.put("</mft:A>", "</font>");
    keyVal.put("</mft:S>", "</font>");
    keyVal.put("</mft:R>", "</font>");
}

public String replaceTag(String replace) {
    for(String key:keyVal.keySet())
        replace=replace.replaceAll(key,keyVal.get(key));
    return replace;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.