当前位置:编程学习 > C#/ASP.NET >>

C# 手机号码隐藏中间四位替换中间四位为星号

C# 隐藏手机号码中间四位数字

使用正则表达式隐藏手机号中间四位
        if (!string.IsNullOrWhiteSpace(txtPhone.Text) &&
                txtPhone.Text.Length == 11)
            {
                txtPhoneDesendent.Text = Regex.Replace(txtPhone.Text, "(\\d{3})\\d{4}(\\d{4})", "$1****$2");
            }


string phone = "15088881234";
string p1 = phone.Substring(0, 3) + "****" + phone.Substring(7, 4);


字符串判断处理 C#中手机号码中间四位变成星号

        /// <summary>
        /// 手机号脱敏
        /// </summary>
        /// <param name="phoneNo"></param>
        /// <returns></returns>
        private string ConvertPhonedNo(string phoneNo)
        {
            if (string.IsNullOrEmpty(phoneNo))
                return phoneNo;
            if (phoneNo.Length < 11)
            {
                return phoneNo;
            }
            StringBuilder sb = new StringBuilder(phoneNo.Substring(0, 3));
            for (int i = 0; i < phoneNo.Length - 5; i++)
            {
                sb.Append('*');
            }
            sb.Append(phoneNo.Substring(phoneNo.Length - 2));
            return sb.ToString();
        }
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,