HTML特殊字符转换
今天发现HTML特殊符号转换还是挺重要的,最近做的这个项目在一些文本框命名时,有时候会输入一些特殊字符,可是当从数据库检索出这些特殊字符回显到画面时,因为是特殊字符所以在画面就不会显示了。我做测试就没测出来这个问题,不得不佩服日本人的仔细啊!不过以后记住就可以了,对于文本框记住回显做个特殊字符转换。希望大家今后不犯和我同样的错误。
public static String getHTMLWithString(String contentC) {String content = contentC;if (content == null) {return "";}content = content.replaceAll("&", "&");content = content.replaceAll("<", "<");content = content.replaceAll(">", ">");content = content.replaceAll(""", """);content = content.replaceAll("\r ", " \n");content = content.replaceAll(" ", " \n");content = content.replaceAll(" ", " ");content = content.replaceAll("'", "'");content = content.replaceAll("!", "!");return content;}public static String getStringWithHTML(String contentC) {String content = contentC;if (content == null) {return "";}content = content.replaceAll("&", "&");content = content.replaceAll("<", "<");content = content.replaceAll(">", ">");content = content.replaceAll(""", """);content = content.replaceAll("\n\r", " ");content = content.replaceAll("\r\n", " ");content = content.replaceAll("\n", " ");content = content.replaceAll(" ", " ");content = content.replaceAll("'", "'");content = content.replaceAll("!", "!");return content;}