国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#隱藏手機號、郵箱等敏感信息的實現方法

2020-01-24 00:59:51
字體:
來源:轉載
供稿:網友

Intro

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

Let's do it !

Method 1:指定左右字符數量

Method 1.1 中間的*的個數和實際長度有關

/// <summary>/// 隱藏敏感信息/// </summary>/// <param name="info">信息實體</param>/// <param name="left">左邊保留的字符數</param>/// <param name="right">右邊保留的字符數</param>/// <param name="basedOnLeft">當長度異常時,是否顯示左邊 /// <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 : 中間的*的個數固定

/// <summary>/// 隱藏敏感信息/// </summary>/// <param name="info">信息實體</param>/// <param name="left">左邊保留的字符數</param>/// <param name="right">右邊保留的字符數</param>/// <param name="basedOnLeft">當長度異常時,是否顯示左邊 /// <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 : “*”數量一定,設置為4個,按信息總長度的比例來取,默認左右各取1/3

/// <summary>/// 隱藏敏感信息/// </summary>/// <param name="info">信息</param>/// <param name="sublen">信息總長與左子串(或右子串)的比例</param>/// <param name="basedOnLeft">當長度異常時,是否顯示左邊,默認true,默認顯示左邊/// <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;}}}

擴展

手機號 1

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

測試結果如下:

手機號 2

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

測試結果如下:

郵件地址

/// <summary>/// 隱藏右鍵詳情/// </summary>/// <param name="email">郵件地址</param>/// <param name="left">郵件頭保留字符個數,默認值設置為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#隱藏手機號、郵箱等敏感信息的實現方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的,在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江孜县| 潮州市| 鄂伦春自治旗| 和政县| 都昌县| 桃园市| 安溪县| 杭州市| 天峻县| 义马市| 定襄县| 班戈县| 天长市| 勐海县| 马山县| 施秉县| 巍山| 新沂市| 宁武县| 西乡县| 增城市| 唐山市| 沾化县| 台北市| 宣恩县| 收藏| 鄂托克前旗| 南昌市| 烟台市| 琼结县| 阜平县| 海南省| 得荣县| 黄平县| 涡阳县| 广南县| 乌审旗| 南川市| 二连浩特市| 西乌| 方山县|