java 转义工具_Java字符转换转义的工具类
}
//轉(zhuǎn)換為html
title/Alt屬性文本,主要轉(zhuǎn)換&,單引號(hào)和雙引號(hào)字符
public static String encode4Title(String value) {
return notNull(value).replaceAll("&",
"&").replaceAll("'",
"'")
.replaceAll(""", """);
}
public static String escape4Sql(String value){
value = notNull(value);
if(value.equals(""))return "";
StringBuffer buffer = new StringBuffer();
if(value.length() > 15)value = value.substring(0,
15);
for (int i = 0; i < value.length(); i++) {
char ch = value.charAt(i);
if(ch == '[')buffer.append("[[]");
else if(ch=='\'')buffer.append("''");
else if(ch=='%')buffer.append("[%]");
else if(ch=='_')buffer.append("[_]");
else if(ch=='^')buffer.append("[^]");
else buffer.append(ch);
}
return buffer.toString();
}
public static String encode4Html(String value) {
value = notNull(value);
if(value.equals(""))return "";
StringBuffer result = new StringBuffer();
for (int i = 0; i < value.length(); i++) {
char ch = value.charAt(i);
if (ch == '
result.append("<");
else if (ch == '&')
result.append("&");
else if (ch == '"')
result.append(""");
else if (ch == '\r')
result.append("
");
else if (ch == '\n') {
if (value.charAt(i - 1) == '\r') {
} else
result.append("
");
} else if (ch == '\t')
result.append("??? ");
else if (ch == ' ')
result.append("?");
else
result.append(ch);
}
return result.toString();
}
//轉(zhuǎn)換非空對(duì)象,防止產(chǎn)生NullPointerException異常
public static String notNull(Object s) {
if (s instanceof String) {
if (s == null || s.toString().trim().equals("")) {
return "";
} else {
return s.toString().trim();
}
} else if (s instanceof Integer) {
if (s == null) {
return "";
} else {
return s.toString();
}
} else {
return "";
}
}
public static String strChinese(String a) {
try {
return new String(a.getBytes("iso-8859-1"));
} catch (UnsupportedEncodingException ex) {
return "";
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java 转义工具_Java字符转换转义的工具类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 特效字幕(ass)简易制作教程
- 下一篇: python Unicode字符与数值转