求教java中有没有方法实现html转义

html-css09

求教java中有没有方法实现html转义,第1张

 public static String html(String content) {

 if(content==null) return ""        

     String html = content

     

 //    html = html.replace( "'", "&apos")

     html = html.replaceAll( "&", "&amp")

     html = html.replace( "\"", "&quot")  //"

     html = html.replace( "\t", "&nbsp&nbsp")// 替换跳格

     html = html.replace( " ", "&nbsp")// 替换空格

     html = html.replace("<", "&lt")

 

     html = html.replaceAll( ">", "&gt")

   

     return html

 }

使用正则表达式即可解决; 例如转义字符\r\n 就只需要如下操作即可: String str = "\r\n"str = str.replaceAll("\r\n","\\r\\n")