1 /// <summary> 2 /// 信息脱敏帮助类 3 /// </summary> 4 public static class DesensitizationHelper 5 { 6 private static string replace(this string s, int len, string data) => s.Remove(len, data.Length).Insert(len, data); 7 /// <summary> 8 /// 手机号脱敏 9 /// </summary> 10 /// <param name="str"></param> 11 /// <returns></returns> 12 public static string DesenseMobilePhone(this string input) 13 { 14 if (string.IsNullOrWhiteSpace(input)) 15 { 16 return ""; 17 } 18 return input.replace(3, "****"); 19 } 20 21 /// <summary> 22 /// 地址脱敏 23 /// </summary> 24 /// <param name="str"></param> 25 /// <returns></returns> 26 public static string DesenseAddress(this string input) 27 { 28 if (string.IsNullOrWhiteSpace(input)) 29 { 30 return ""; 31 } 32 return Regex.Replace(input, @"\d", "*"); 33 } 34 35 /// <summary> 36 /// 姓名脱敏 37 /// </summary> 38 /// <param name="str"></param> 39 /// <returns></returns> 40 public static string DesenseConsignee(this string input) 41 { 42 char specialChar = ‘*‘; 43 if (string.IsNullOrWhiteSpace(input)) 44 { 45 return ""; 46 } 47 48 if (input.Length > 0 && input.Length <= 2) 49 input = input.Substring(0, 1) + "*"; 50 else if (input.Length > 2) 51 { 52 int lenth = input.Length - 2;//掐头去尾 53 string specialStr = string.Empty.PadRight(lenth, specialChar); 54 input = input.replace(1, specialStr); 55 } 56 return input; 57 } 58 }
原文:https://www.cnblogs.com/nanxixi/p/14718524.html