fork download
  1. import java.util.Map;
  2. import java.util.regex.Pattern;
  3.  
  4. class Main {
  5.  
  6. public static void main(String[] args) {
  7. String text = "hello world, I have an apple, an applet and a pear.";
  8. String result = censor(text);
  9. System.out.println(result);
  10. }
  11.  
  12. private static String censor(String text) {
  13. Map<String, String> filters = Map.of(
  14. "hello", "h*llo",
  15. "world", "w*rld",
  16. "apple", "*****"
  17. );
  18.  
  19. for (var filter : filters.entrySet()) {
  20. String pattern = "\\b" + Pattern.quote(filter.getKey()) + "\\b";
  21. text = text.replaceAll(pattern, filter.getValue());
  22. }
  23. return text;
  24. }
  25.  
  26. }
  27.  
Success #stdin #stdout 0.12s 54324KB
stdin
Standard input is empty
stdout
h*llo w*rld, I have an *****, an applet and a pear.