ASCII到HTML实体在Java中转义 [英] ASCII to HTML-Entities Escaping in 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("●"); break;
case '\u25BA': sb.append("►"); 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屋!