字符串隐藏部分子串
private void hide(Object obj, int[] lengths, int[] indexs, String... fieldNames) throws Exception {
if (fieldNames.length != lengths.length) {
return;
}
if (fieldNames.length != indexs.length) {
return;
}
for (int i = 0; i < fieldNames.length; i++) {
this.doHide(obj, fieldNames[i], lengths[i], indexs[i]);
}
}
private void doHide(Object obj, String fieldName, int length, int start) throws Exception {
Field field = ListTool.getDeclaredField(obj.getClass(), fieldName);
if (field != null) {
if (field.getType().equals(String.class)) {
field.setAccessible(true);
Object value = field.get(obj);
if (ObjectUtil.isNotEmpty(value)) {
String realValue = value.toString();
if (realValue.length() <= length) {
return;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append("*");
}
if (start == -1) {
start = (realValue.length() - length) / 2;
}
int end = start + length;
if (end >= realValue.length()) {
end = realValue.length() - 1;
}
realValue = realValue.substring(0, start) + sb.toString() + realValue.substring(end);
field.set(obj, realValue);
}
}
}
}
this.hide(mjxx, new int[]{4, 12}, new int[]{-1, 2}, "phonenumber", "zjhm");