日志增加string字符串格式化标签包括转义,截取,null字符处理
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,6 +354,12 @@
|
||||
<example>${fns:getPermissionByNo(str1,list)}</example>
|
||||
</function>
|
||||
|
||||
|
||||
<function>
|
||||
<description>字符串转义及截取,null转""</description>
|
||||
<name>stringFormat</name>
|
||||
<function-class>com.nis.util.StringUtils</function-class>
|
||||
<function-signature>java.lang.String stringFormat(java.lang.String, int)</function-signature>
|
||||
<example>${fns:stringFormat(str,len)}</example>
|
||||
</function>
|
||||
|
||||
</taglib>
|
||||
|
||||
Reference in New Issue
Block a user