Intro
When doing the project, there are some sensitive information on the page, you need to use "*" to hide some of the more important information, so you intend to write a common method.
Let's do it!
Method 1: Specify the number of left and right characters
Method 1.1 The number of * in the middle and the actual length of
<summary>///Hidden Sensitive information///</summary>///<param name= "info" > Information entity </param>///<param name= "L EFT > Left characters </param>///<param name= "right" > reserved characters </param>///<param name= "Basedonleft" > Show left///<code>true</code> show left,<code>false</code> display right///</param>///when length is abnormal <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: The number of intermediate * is fixed
<summary>///Hidden Sensitive information///</summary>///<param name= "info" > Information entity </param>///<param name= "L EFT > Left characters </param>///<param name= "right" > reserved characters </param>///<param name= "Basedonleft" > When the length is abnormal, show left///<code>true</code> show left,<code>false</code> display right///<returns>< /returns> public static string HideSensitiveInfo1 (string info, int. left, int right, bool Basedonleft = True) {if stri Ng.
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: "*" Quantity must be set to 4, according to the total length of information to take, the default left and right 1/3
<summary>///Hidden Sensitive information///</summary>///<param name= "info" > Information </param>///<param name= "Sub Len "> The ratio of the total length of the information to the Zoozi string (or right substring) </param>///<param name=" Basedonleft "> when the length is abnormal, whether to show the left side, default true, the default display left///< Code>true</code> Show left,<code>false</code> display right///<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; }
}
}
Extended
Mobile phone Number 1
<summary>
///Hidden mobile number details
///</summary>
///<param name= "Phone" > Mobile number </param>
///<param name= "left" > reserved characters </param>
///<param name= "right" > reserved character number </param>
///<returns></returns> public
static string hideteldetails (string phone, int left = 3, int right = 4)
{return
hidesensitiveinfo (phone, left, right);
}
The test results are as follows:
Mobile phone Number 2
<summary>
///Hidden mobile number details
///</summary>
///<param name= "Phone" > Mobile number </param>
///<param name= "left" > reserved characters </param>
///<param name= "right" > reserved character number </param>
///<returns></returns> public
static string hideteldetails (string phone, int left = 3, int right = 4 )
{return
HideSensitiveInfo1 (phone, left, right);
}
The test results are as follows:
Mailing Address
<summary>
///Hidden right key details
///</summary>
///<param name= "Email" > Email address </param>
///<param name= "left" > message headers retain the number of characters, the default value is set to 3</param>
///<returns></returns>
public static string Hideemaildetails (string e-mail, int left = 3)
{
if (string.isnullorempty (email))
{ return
"";
}
if (System.Text.RegularExpressions.Regex.IsMatch (email, @ "\w+" [-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+)//If it is the mail address
{
int suffixlen =email. Length-email. LastIndexOf (' @ ');
return Hidesensitiveinfo (email, left, suffixlen,false);
else
{return
hidesensitiveinfo (email);
}
}
The test results are as follows:
The above is a small series to introduce the C # hidden mobile phone number, mailbox and other sensitive information to achieve the method, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for your support cloud Habitat community site!