隐藏手机号、邮箱等敏感信息Intro做项目的时候,页面上有一些敏感信息,需要用“*”隐藏一些比较重要的信息,于是打算写一个通用的方法。Lets do it !Method 1:指定左右字符数量Method 1.1 中间的*的个数和实际长度 ...
隐藏手机号、邮箱等敏感信息
Intro
做项目的时候,页面上有一些敏感信息,需要用“*”隐藏一些比较重要的信息,于是打算写一个通用的方法。
Let's do it !
Method 1:指定左右字符数量
Method 1.1 中间的*的个数和实际长度有关
1 /// <summary> 2 /// 隐藏敏感信息 3 /// </summary> 4 /// <param name="info">信息实体</param> 5 /// <param name="left">左边保留的字符数</param> 6 /// <param name="right">右边保留的字符数</param> 7 /// <param name="basedOnLeft">当长度异常时,是否显示左边 8 /// <code>true</code>显示左边,<code>false</code>显示右边 9 /// </param>10 /// <returns></returns>11 public static get='_blank'>string HideSensitiveInfo(string info, int left, int right, bool basedOnLeft=true)12 {13 if (String.IsNullOrEmpty(info))14 {15 return "";16 }17 StringBuilder sbText = new StringBuilder();18 int hiddenCharCount = info.Length - left - right;19 if (hiddenCharCount > 0)20 {21 string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);22 sbText.Append(prefix);23 for (int i = 0; i < hiddenCharCount; i++)24 {25 sbText.Append("*");26 }27 sbText.Append(suffix);28 }29 else30 {31 if (basedOnLeft)32 {33 if (info.Length > left && left > 0)34 {35 sbText.Append(info.Substring(0, left) + "****");36 }37 else38 {39 sbText.Append(info.Substring(0, 1) + "****");40 }41 }42 else43 {44 if (info.Length > right && right > 0)45 {46 sbText.Append("****" + info.Substring(info.Length - right));47 }48 else49 {50 sbText.Append("****" + info.Substring(info.Length - 1));51 }52 }53 }54 return sbText.ToString();55 }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:隐藏手机号、邮箱等敏感信息
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。