ASCII到HTML实体在Java中转义 [英] ASCII to HTML-Entities Escaping in Java

查看:230
本文介绍了ASCII到HTML实体在Java中转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个网站带有转义码,我只是想知道有人是否已经做到了这一点,所以我不用花几个小时来构建这个逻辑:

  StringBuffer sb = new StringBuffer(); 
int n = s.length(); (int i = 0; i< n; i ++)
{
char c = s.charAt(i);
switch(c){
case'\\\●':sb.append(&#9679;);打破;
case'\\\►':sb.append(&#9658;);打破;

/ *
...其余的十六进制字符文字到HTML实体
* /

默认值:sb.append(c);打破;
}
}


解决方案

codes是实际字符的unicode值的一个十进制表示形式。在我看来,像这样的东西可以工作,除非你想要对哪些代码进行转换是非常严格的,哪些不是。 c $ c> StringBuilder sb = new StringBuilder();
int n = s.length(); (int i = 0; i< n; i ++)
{
char c = s.charAt(i);
if(Character.UnicodeBlock.of(c)!= Character.UnicodeBlock.BASIC_LATIN){
sb.append(&#);
sb.append((int)c);
sb.append(';');
} else {
sb.append(c);
}

}


I found this website with escape codes and I'm just wondering if someone has done this already so I don't have to spend couple of hours building this logic:

 StringBuffer sb = new StringBuffer();
 int n = s.length();
 for (int i = 0; i < n; i++) {
     char c = s.charAt(i);
     switch (c) {
         case '\u25CF': sb.append("&#9679;"); break;
         case '\u25BA': sb.append("&#9658;"); break;

         /*
         ... the rest of the hex chars literals to HTML entities
         */  

         default:  sb.append(c); break;
     }
 }

解决方案

These "codes" is a mere decimal representation of the unicode value of the actual character. It seems to me that something like this would work, unless you want to be very strict about which codes get converted, and which don't.

StringBuilder sb = new StringBuilder();
 int n = s.length();
 for (int i = 0; i < n; i++) {
     char c = s.charAt(i);
     if (Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN) {
        sb.append("&#");
        sb.append((int)c);
        sb.append(';');
     } else {
        sb.append(c);
     }

 }

这篇关于ASCII到HTML实体在Java中转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆