最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#隱藏手機(jī)號(hào)、郵箱等敏感信息的實(shí)現(xiàn)方法

 更新時(shí)間:2016年09月15日 11:57:56   作者:WeihanLi  
這篇文章主要介紹了C#隱藏手機(jī)號(hào)、郵箱等敏感信息的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下

Intro

做項(xiàng)目的時(shí)候,頁面上有一些敏感信息,需要用“*”隱藏一些比較重要的信息,于是打算寫一個(gè)通用的方法。

Let's do it !

Method 1:指定左右字符數(shù)量

Method 1.1 中間的*的個(gè)數(shù)和實(shí)際長(zhǎng)度有關(guān)

/// <summary>
/// 隱藏敏感信息
/// </summary>
/// <param name="info">信息實(shí)體</param>
/// <param name="left">左邊保留的字符數(shù)</param>
/// <param name="right">右邊保留的字符數(shù)</param>
/// <param name="basedOnLeft">當(dāng)長(zhǎng)度異常時(shí),是否顯示左邊 
/// <code>true</code>顯示左邊,<code>false</code>顯示右邊
/// </param>
/// <returns></returns>
public static string HideSensitiveInfo(string info, int left, int right, bool basedOnLeft=true)
{
if (String.IsNullOrEmpty(info))
{
return "";
}
StringBuilder sbText = new StringBuilder();
int hiddenCharCount = info.Length - left - right;
if (hiddenCharCount > 0)
{
string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
sbText.Append(prefix);
for (int i = 0; i < hiddenCharCount; i++)
{
sbText.Append("*");
}
sbText.Append(suffix);
}
else
{
if (basedOnLeft)
{
if (info.Length > left && left > 0)
{
sbText.Append(info.Substring(0, left) + "****");
}
else
{
sbText.Append(info.Substring(0, 1) + "****");
}
}
else
{
if (info.Length > right && right > 0)
{
sbText.Append("****" + info.Substring(info.Length - right));
}
else
{
sbText.Append("****" + info.Substring(info.Length - 1));
}
}
}
return sbText.ToString();
}

Method 1.2 : 中間的*的個(gè)數(shù)固定

/// <summary>
/// 隱藏敏感信息
/// </summary>
/// <param name="info">信息實(shí)體</param>
/// <param name="left">左邊保留的字符數(shù)</param>
/// <param name="right">右邊保留的字符數(shù)</param>
/// <param name="basedOnLeft">當(dāng)長(zhǎng)度異常時(shí),是否顯示左邊 
/// <code>true</code>顯示左邊,<code>false</code>顯示右邊
/// <returns></returns>
public static string HideSensitiveInfo1(string info, int left, int right, bool basedOnLeft = true)
{
if (String.IsNullOrEmpty(info))
{
return "";
}
StringBuilder sbText = new StringBuilder();
int hiddenCharCount = info.Length - left - right;
if (hiddenCharCount > 0)
{
string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
sbText.Append(prefix);
sbText.Append("****");
sbText.Append(suffix);
}
else
{
if (basedOnLeft)
{
if (info.Length > left && left >0)
{
sbText.Append(info.Substring(0, left) + "****");
}
else
{
sbText.Append(info.Substring(0, 1) + "****");
}
}
else
{
if (info.Length > right && right>0)
{
sbText.Append("****" + info.Substring(info.Length - right));
}
else
{
sbText.Append("****" + info.Substring(info.Length - 1));
}
}
}
return sbText.ToString();
}

Method 2 : “*”數(shù)量一定,設(shè)置為4個(gè),按信息總長(zhǎng)度的比例來取,默認(rèn)左右各取1/3

/// <summary>
/// 隱藏敏感信息
/// </summary>
/// <param name="info">信息</param>
/// <param name="sublen">信息總長(zhǎng)與左子串(或右子串)的比例</param>
/// <param name="basedOnLeft">當(dāng)長(zhǎng)度異常時(shí),是否顯示左邊,默認(rèn)true,默認(rèn)顯示左邊
/// <code>true</code>顯示左邊,<code>false</code>顯示右邊
/// <returns></returns>
public static string HideSensitiveInfo(string info,int sublen = 3,bool basedOnLeft = true)
{
if (String.IsNullOrEmpty(info))
{
return "";
}
if (sublen<=1)
{
sublen = 3;
}
int subLength = info.Length / sublen;
if (subLength > 0 && info.Length > (subLength*2) )
{
string prefix = info.Substring(0, subLength), suffix = info.Substring(info.Length - subLength);
return prefix + "****" + suffix;
}
else
{
if (basedOnLeft)
{
string prefix = subLength > 0 ? info.Substring(0, subLength) : info.Substring(0, 1);
return prefix + "****";
}
else
{
string suffix = subLength > 0 ? info.Substring(info.Length-subLength) : info.Substring(info.Length-1);
return "****"+suffix;
}
}
}

擴(kuò)展

手機(jī)號(hào) 1

/// <summary>
/// 隱藏手機(jī)號(hào)詳情
/// </summary>
/// <param name="phone">手機(jī)號(hào)</param>
/// <param name="left">左邊保留字符數(shù)</param>
/// <param name="right">右邊保留字符數(shù)</param>
/// <returns></returns>
public static string HideTelDetails(string phone, int left = 3, int right = 4)
{
return HideSensitiveInfo(phone, left, right);
}

測(cè)試結(jié)果如下:

手機(jī)號(hào) 2

/// <summary>
/// 隱藏手機(jī)號(hào)詳情
/// </summary>
/// <param name="phone">手機(jī)號(hào)</param>
/// <param name="left">左邊保留字符數(shù)</param>
/// <param name="right">右邊保留字符數(shù)</param>
/// <returns></returns>
public static string HideTelDetails(string phone, int left = 3, int right = 4)
{
return HideSensitiveInfo1(phone, left, right);
}

測(cè)試結(jié)果如下:

郵件地址

/// <summary>
/// 隱藏右鍵詳情
/// </summary>
/// <param name="email">郵件地址</param>
/// <param name="left">郵件頭保留字符個(gè)數(shù),默認(rèn)值設(shè)置為3</param>
/// <returns></returns>
public static string HideEmailDetails(string email, int left = 3)
{
if (String.IsNullOrEmpty(email))
{
return "";
}
if (System.Text.RegularExpressions.Regex.IsMatch(email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))//如果是郵件地址
{
int suffixLen =email.Length - email.LastIndexOf('@');
return HideSensitiveInfo(email, left, suffixLen,false);
}
else
{
return HideSensitiveInfo(email);
}
}

測(cè)試結(jié)果如下:

以上所述是小編給大家介紹的C#隱藏手機(jī)號(hào)、郵箱等敏感信息的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

凤凰县| 辰溪县| 尼木县| 乐山市| 栾川县| 东兰县| 鹤峰县| 东乌珠穆沁旗| 北京市| 青冈县| 禄丰县| 渭源县| 淄博市| 河曲县| 兴业县| 平定县| 清苑县| 池州市| 凤山县| 新营市| 嘉鱼县| 汝阳县| 西林县| 东城区| 汉阴县| 宁都县| 嘉定区| 吉林市| 昭通市| 萍乡市| 邓州市| 湖北省| 襄汾县| 大同市| 怀安县| 岳池县| 松滋市| 潜江市| 柘荣县| 禄丰县| 郧西县|