1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| class Solution { private static final Map<String, String> map = new HashMap<>(){{ put(""", "\""); put("'", "'"); put("&", "&"); put(">", ">"); put("<", "<"); put("⁄", "/"); }};
public String entityParser(String text) { StringBuilder builder = new StringBuilder(); int n = text.length(); for (int i = 0; i < n; ++i) { char c = text.charAt(i); if (c == '&') { int j = i + 1; boolean flag = false; for (; j < n && text.charAt(j) != ';'; ++j) { if (text.charAt(j) == '&') { builder.append(text, i, j); i = j - 1; flag = true; break; } } if (flag) continue; String s = text.substring(i, j == n ? n : j + 1); i = j; s = map.getOrDefault(s, s); builder.append(s); } else { builder.append(c); } } return builder.toString(); } }
|