📄 htmlencoder.java
字号:
package cn.myapps.util;
/**
* The html encode utility.
*/
public class HtmlEncoder {
private static final String[] htmlCode = new String[256];
static {
for (int i = 0; i < 10; i++) {
htmlCode[i] = "�" + i + ";";
}
for (int i = 10; i < 32; i++) {
htmlCode[i] = "�" + i + ";";
}
for (int i = 32; i < 128; i++) {
htmlCode[i] = String.valueOf((char) i);
}
htmlCode['\"'] = """;
htmlCode['&'] = "&";
htmlCode['<'] = "<";
htmlCode['>'] = ">";
htmlCode[' '] = " ";
for (int i = 128; i < 256; i++) {
htmlCode[i] = "&#" + i + ";";
}
}
/**
* Encode the given text into html.
* @param s The text to encode
* @return The encoded string
*/
public static String encode(String s) {
int n = s.length();
char character;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < n; i++) {
character = s.charAt(i);
try {
buffer.append(htmlCode[character]);
} catch (ArrayIndexOutOfBoundsException aioobe) {
buffer.append(character);
}
}
return buffer.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -