runningRain

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

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

            /// <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();
            }
    

分类:

技术点:

相关文章:

  • 2021-11-15
  • 2021-11-25
  • 2021-12-07
  • 2021-11-13
  • 2021-12-07
  • 2021-11-25
  • 2021-08-26
猜你喜欢
  • 2021-11-25
  • 2022-01-28
相关资源
相似解决方案