身份证脱敏
public static String hideIdNo(String realNo) { if (realNo == null) { return null; } String newNo = ""; if (realNo.length() == 15) { newNo = realNo.replaceAll("(\\d{4})\\d{7}(\\d{4})", "$1*******$2"); } if (realNo.length() == 18) { newNo = realNo.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1**********$2"); } return newNo; }
姓名脱敏
public static String hideName(String realname) { if (realname == null) { return null; } char[] r = realname.toCharArray(); String name = ""; if (r.length == 1) { name = realname; } if (r.length == 2) { name = realname.replaceFirst(realname.substring(1), "*"); } if (r.length > 2) { name = realname.replaceFirst(realname.substring(1, r.length - 1), "*"); } return name; }
手机号脱敏
public static String hidePhone(String realPhone) { if (realPhone == null) { return null; } String phoneNumber; if (realPhone.length() == 11) { phoneNumber = realPhone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); } else { phoneNumber = realPhone; } return phoneNumber; }