68 lines
1.6 KiB
C#
68 lines
1.6 KiB
C#
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
|
|
/// <summary>
|
|
/// 個資遮罩
|
|
/// 乎叫方式:MyWeb.Mask.GetPersonalMask("王小明", "*");
|
|
/// </summary>
|
|
///
|
|
namespace MyWeb
|
|
{
|
|
public class Mask
|
|
{
|
|
public Mask()
|
|
{
|
|
|
|
}
|
|
|
|
static public string GetPersonalMask(string OriStr, string oPrefix)
|
|
{
|
|
return GetPersonalMask(OriStr, oPrefix, true);
|
|
}
|
|
|
|
|
|
static private string GetPersonalMask(string OriStr, string oPrefix, bool IsDis)
|
|
{
|
|
string oStr = "";
|
|
OriStr = OriStr.Trim();
|
|
char[] oStrArry = OriStr.ToCharArray();
|
|
int[] oArray = new int[] { 0, OriStr.Trim().Length - 1 };
|
|
|
|
if (Regex.IsMatch(OriStr.Trim(), @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$"))
|
|
{
|
|
oStr += GetPersonalMask(OriStr.Split('@')[0], oPrefix) + "@";
|
|
|
|
for (int i = 0; i < OriStr.Split('@')[1].Split('.').Length; i++)
|
|
{
|
|
string oStrL = OriStr.Split('@')[1].Split('.')[i].ToString();
|
|
if (i == 0)
|
|
oStr += GetPersonalMask(oStrL, oPrefix, false);
|
|
else
|
|
oStr += "." + GetPersonalMask(oStrL, oPrefix, false);
|
|
}
|
|
return oStr;
|
|
}
|
|
else if (Regex.IsMatch(OriStr.Trim(), "^(09([0-9]){8})$"))
|
|
{
|
|
oArray = new int[] { 0, 1, 2, 7, 8, 9 };
|
|
}
|
|
else if (Regex.IsMatch(OriStr.Trim(), "^[a-zA-Z][0-9]{9}$"))
|
|
{
|
|
oArray = new int[] { 0, 1, 2, 3, 9 };
|
|
}
|
|
|
|
for (int i = 0; i < oStrArry.Length; i++)
|
|
{
|
|
if (IsDis)
|
|
oStr += oArray.Contains(i) ? oStrArry[i].ToString() : oPrefix;
|
|
else
|
|
oStr += oArray.Contains(i) ? oPrefix : oStrArry[i].ToString();
|
|
}
|
|
return oStr;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|