正则表达式隐藏手机号中间4位数字 手机号、身份证脱敏

时间:2024-03-08 09:51:48

用正则替换

response.Member.LoginMobile = Regex.Replace(queryResult.Member.LoginMobile, @"(?im)(\d{3})(\d{4})(\d{4})", "$1****$3");  //13431230555变成134****0555

不用正则,用字符串替换:

var name = queryResult.Member.LoginMobile.Substring(4, 4);
response.Member.LoginMobile = queryResult.Member.LoginMobile.Replace(name, "****");

 

名字隐藏间字符

         if (name1.Length > 2)
         {
                string result = name1.Substring(0, 1).PadRight(name1.Substring(1).Length, \'*\') + name1.Substring(name1.Length - 1);
                Console.WriteLine(result);
            }
            if (name1.Length > 1 && name1.Length <= 2)
            {
                Console.WriteLine(name1.Substring(0,1)+"*");
            }

 

手机号码和身份证前三后四脱敏

public static String mobileEncrypt(String mobile){
if(TextUtils.isEmpty(mobile) || (mobile.length() != 11)){
return mobile;
}
return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
}

public static String idEncrypt(String id){
if(TextUtils.isEmpty(id) || (id.length() < 8)){
return id;
}
return id.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
}