asp.net格式化电话号码为星号(*)

asp.net如何把电话号码格式化为星号(*),如11位手机号中间四们显示为*,防止用户的隐私泄漏。

public string getRoleTel(string tel)
{
      Regex re = new Regex("", RegexOptions.None);
      int tellen = tel.Length;
      switch (tellen)
      {
          case 5:
              re = new Regex("(\\d{3})(\\d{2})", RegexOptions.None);
              tel = re.Replace(tel, "$1****");
              break;
          case 6:
              re = new Regex("(\\d{3})(\\d{3})", RegexOptions.None);
              tel = re.Replace(tel, "$1****");
              break;
          case 7:
              re = new Regex("(\\d{3})(\\d{4})", RegexOptions.None);
              tel = re.Replace(tel, "$1****");
              break;
          case 8:
              re = new Regex("(\\d{3})(\\d{4})(\\d{1})", RegexOptions.None);
              tel = re.Replace(tel, "$1****$3");
              break;
          case 9:
              re = new Regex("(\\d{3})(\\d{4})(\\d{2})", RegexOptions.None);
              tel = re.Replace(tel, "$1****$3");
              break;
          case 10:
              re = new Regex("(\\d{3})(\\d{4})(\\d{3})", RegexOptions.None);
              tel = re.Replace(tel, "$1****$3");
              break;
          case 11:
              re = new Regex("(\\d{3})(\\d{4})(\\d{4})", RegexOptions.None);
              tel = re.Replace(tel, "$1****$3");
              break;
          case 12:
              re = new Regex("(\\d{3})(\\d{4})(\\d{5})", RegexOptions.None);
              tel = re.Replace(tel, "$1****$3");
              break;
          default:
              re = new Regex("(\\d{3})(\\d{4})(\\d{13})", RegexOptions.None);
              tel = re.Replace(tel, "$1****$3");
              break;
      }           
      return tel;
}

虽说这里是演示的电话号码,但是其它字符串你照样可以扩展来进行格式化,如身份证号码等信息。

You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注