The goal is I am reading in a text file and finding certain words to replace. It will then replace the words and output a new text file that has the words replaced. My code works good for single words, but if I try to replace a phrase with a space, it doesn't work. What I have is a HashMap that contains what I need to search for in the file.
HashMap<String, Integer> hm = new HashMap<>(); hm.put("null",0); hm.put("max",1); hm.put("Do not repeat",2); hm.put("names",3); I then iterate through the HashMap and replace the strings with the word if the file contains it.
for (String key : hm.keySet()) { String check = key; System.out.println(check); text = text.toLowerCase(Locale.ROOT).replaceAll(check, "WRONG"); } String new = text; This isn't working if I have a space in the words like for "Do not repeat". How can I get this to work for phrases and not just single words? It completely skips over the phrases and outputs the new file with only the single words replaced.
Pattern.quote(check), to avoid surprising things with metacharacters. Or just usereplace, ofc.