0

egg:

String str = "Don't trouble trouble until trouble troubles you"
Map<String,String> replacementMap = new HashMap<String,String>(){{put("trouble","replaceStr");}};

I want the output was:

"Don't troubleStr troubleStr until troubleStr troubles you."

The scene is i have a paragraph and i also have a keywords database ,when i show the paragraph in web page,i want to highlight the matched keywords and replace a link target.

kalman
  • 41
  • 1
  • 5
  • A similar question has been answered before: [Replace String values with value in Hash Map](https://stackoverflow.com/questions/18601011/replace-string-values-with-value-in-hash-map) – Jacob G. Apr 02 '20 at 03:43
  • You need to show a more accurate example of what effect you want on the String input and show some effort in coding a solution, or your question will be closed as a duplicate. – Gilbert Le Blanc Apr 02 '20 at 06:25

1 Answers1

0
  String str = "Don't trouble trouble until trouble troubles you";
    Map<String,String> replacementMap = new HashMap<String, String>(){{put("trouble","replaceStr");}};
    for (Map.Entry<String, String> entry : replacementMap.entrySet()) {
        System.out.println(entry.getKey()); //trouble
        System.out.println(entry.getValue()); // replaceStr
        str = str.replace(entry.getKey(), entry.getValue());
    }
    System.out.println(str);
hesh
  • 11
  • 3
  • The scene is i have a paragraph and i also have a keywords database ,when i show the paragraph in web page,i want to highlight the matched keywords and replace a link target.Ablove maybe too simple for my scene. – kalman Apr 02 '20 at 04:11
  • what do u mean by replace link target ? – hesh Apr 02 '20 at 04:52