@Test
public void testAddition() {
encrypt("111111111111111111", 6, 4);//该身份证号码为测试,并不实际存在
// 电话号码测试
encrypt("18888888888", 3, 2);//该电话号码为测试,并不实际存在
}
/**
* @param front 需要显示前几位
* @param end 需要显示末几位
* @return 处理完成之后的结果
*/
public static String encrypt(String idCard, int front, int end) {
if (StringUtils.isEmpty(idCard)) return idCard;
if ((front + end) > idCard.length()) return idCard;
if (front < 0 || end < 0) return idCard;
//计算 ‘*’ 号数量
int asteriskCount = idCard.length() - (front + end);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < asteriskCount; i++) {
stringBuilder.append("*");
}
String regex = "(\\w{" + String.valueOf(front) + "})(\\w+)(\\w{" + String.valueOf(end) + "})";
return idCard.replaceAll(regex, "$1" + stringBuilder + "$3");
}
标签:end,String,int,截取,idCard,return,front,Java,涉密
From: https://blog.51cto.com/u_16297579/9114052