用正则替换
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)+"*"); }