正則表達式隱藏手機號中間4位數字 手機號、身份證脫敏


用正則替換

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})", "*");
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM