http://t.zoukankan.com/YYkun-p-15380859.html
C# 实现数据脱敏
数据脱敏,此处以姓名、身份证为例 /// <summary> /// 姓名敏感处理 /// </summary> /// <param name="fullName">姓名</param> /// <returns>脱敏后的姓名</returns> public string SetSensitiveName(string fullName) { if (string.IsNullOrEmpty(fullName)) return string.Empty; string familyName = fullName.Substring(0, 1); string end = fullName.Substring(fullName.Length - 1, 1); string name = string.Empty; //长度为2 if (fullName.Length <= 2) name = familyName + "*"; //长度大于2 else if (fullName.Length >= 3) { name = familyName.PadRight(fullName.Length - 1, '*') + end; } return name; } /// <summary> /// 身份证脱敏 /// </summary> /// <param name="idCardNo">身份证号</param> /// <returns>脱敏后的身份证号</returns> private string SetSensitiveIdCardNo(string idCardNo) { if (string.IsNullOrEmpty(idCardNo) || (idCardNo.Length != 15 && idCardNo.Length != 18)) return idCardNo; string begin = idCardNo.Substring(0, 6); string middle = idCardNo.Substring(6, 8); string end = idCardNo.Substring(14, idCardNo.Length - 14); string card = string.Empty; card = begin + "********" + end; return card; } 测试: string no = SetSensitiveIdCardNo("610253199804230067"); //string no = SetSensitiveIdCardNo("610253199804230"); string name = SetSensitiveName("齐天大圣孙悟空"); MessageBox.Show("idcardno==" + no + " name==" + name);
标签:string,c#,Length,身份证,fullName,idCardNo,脱敏,name From: https://www.cnblogs.com/Dongmy/p/16877929.html