姓名、身份证号、银行卡号数据脱敏处理

package view;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataDesensitize {
/**
* 中文名字:只展示姓名第一个字
* 身份证号:年月日脱敏,前6后4不脱敏
* 手机号:前三后四不脱敏,中间脱敏
* 银行卡:前6位和后4位不脱敏,中间脱敏
/
private static String replaceStr(String param) {
int len=param.length();
if(isContainChinese(param)){
return param.replaceAll("(.{1})(.
)(.{0})", "3");
}
return param.replaceAll("(.{"+(len<12?3:6)+"})(.*)(.{4})", "3");
}

/**
 * @description:
 * @return: 判断字符串中是否含中文
 * @author: Administrator
 * @date: 2019/10/4
 */
private static boolean isContainChinese(String str) {
    Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    Matcher m = p.matcher(str);
    if (m.find()) {
        return true;
    }
    return false;
}

public static void main(String[] args) {
    String name = "到桌源码";
    System.out.println(replaceStr(name));
    String id = "551254198807213368";
    System.out.println(replaceStr(id));
    String bankId = "6225337608836806";
    System.out.println(replaceStr(bankId));
}

}

你可能感兴趣的:(姓名、身份证号、银行卡号数据脱敏处理)