private final static int NAME_LEN_MIN = 2;
private final static int NAME_LEN_MAX = 18;
private final static int MAX_THREE_UTF8 = 0xF0;
private final static int BYTE_MASK = 0xFF;
private final static byte[] DEFAULT_REPLACE = "".getBytes();
private final static int FOLLOW_BYTE_MIN = 0x80;
private final static int FOLLOW_BYTE_MAX = 0xBF;
public static String filterUTF8OutThree(String vStr, String replace) {
if (StringUtils.isBlank(vStr)) {
return ".";
}
byte[] tStrBytes = vStr.getBytes();
List<Byte> tResultList = new ArrayList<>();
byte[] replaceByte = replace == null ? DEFAULT_REPLACE : replace.getBytes();
boolean tRemoveSign = false;
for (byte tStrByte : tStrBytes) {
int tTmpByte = tStrByte & BYTE_MASK;
if (MAX_THREE_UTF8 <= tTmpByte) {
tRemoveSign = true;
for (byte tChangeByte : replaceByte) {
tResultList.add(tChangeByte);
}
} else if (FOLLOW_BYTE_MIN <= tTmpByte && FOLLOW_BYTE_MAX >= tTmpByte) {
if (false == tRemoveSign)
tResultList.add(tStrByte);
} else {
tRemoveSign = false;
tResultList.add(tStrByte);
}
}
byte[] tResultBytes = new byte[tResultList.size()];
for (int i = 0; i < tResultList.size(); i++) {
tResultBytes[i] = tResultList.get(i);
}
try {
return new String(tResultBytes, "utf-8");
} catch (UnsupportedEncodingException e) {
log.warn("字符串转utf-8失败!!! str={}", vStr, e);
return vStr;
}
}