日志增加string字符串格式化标签包括转义,截取,null字符处理

This commit is contained in:
zhanghongqing
2018-08-01 10:26:29 +08:00
parent 6bd5be9722
commit 7f05359125
2 changed files with 35 additions and 1 deletions

View File

@@ -472,4 +472,32 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return true;
}
public static String stringFormat(String str,int len){
if(str.trim().equals("null")||str==null){
return "";
}else{
str = Encodes.escapeHtml(str);
if(len!=-1){
try {
StringBuilder sb = new StringBuilder();
int currentLength = 0;
for (char c : str.toCharArray()) {
currentLength += String.valueOf(c).getBytes("GBK").length;
if (currentLength <= len - 3) {
sb.append(c);
} else {
sb.append("...");
break;
}
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return str;
}
}
}