java特殊字符转html,html特殊字符转换(java)

  • Post author:
  • Post category:java


/**

* 把文本编码为Html代码

* @param target

* @return 编码后的字符串

*/

public static String htmEncode(String target)

{

StringBuffer stringbuffer = new StringBuffer();

int j = target.length();

for (int i = 0; i < j; i++)

{

char c = target.charAt(i);

switch (c)

{

case 60:

stringbuffer.append(”

break;

case 62:

stringbuffer.append(“>”);

break;

case 38:

stringbuffer.append(“&”);

break;

case 34:

stringbuffer.append(“””);

break;

case 169:

stringbuffer.append(“©”);

break;

case 174:

stringbuffer.append(“®”);

break;

case 165:

stringbuffer.append(“¥”);

break;

case 8364:

stringbuffer.append(“€”);

break;

case 8482:

stringbuffer.append(“™”);

break;

case 13:

if (i < j – 1 && target.charAt(i + 1) == 10)

{

stringbuffer.append(”

“);

i++;

}

break;

case 32:

if (i < j – 1 && target.charAt(i + 1) == ‘ ‘)

{

stringbuffer.append(”  “);

i++;

break;

}

default:

stringbuffer.append(c);

break;

}

}

retur