Hide sensitive information such as mobile phone number and email address, and hide mobile phone number and email address
Hiding sensitive information such as mobile phone numbers and mailboxes
During project creation, some sensitive information on the page needs to be hidden with "*", so we plan to write a general method.
Let's do it!
Method 1: specifies the number of left and right characters. The number of * In The Middle Of Method 1.1 is related to the actual length
1 /// <summary>
2 /// Hide sensitive information
3 /// </ summary>
4 /// <param name = "info"> information entity </ param>
5 /// <param name = "left"> the number of characters left to reserve </ param>
6 /// <param name = "right"> Number of characters reserved on the right </ param>
7 /// <param name = "basedOnLeft"> when the length is abnormal, whether to display the left
8 /// <code> true </ code> shows the left, <code> false </ code> shows the right
9 /// </ param>
10 /// <returns> </ returns>
11 public static 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 ++)
twenty four {
25 sbText.Append ("*");
26}
27 sbText.Append (suffix);
28}
29 else
30 {
31 if (basedOnLeft)
32 {
33 if (info.Length> left && left> 0)
34 {
35 sbText.Append (info.Substring (0, left) + "****");
36}
37 else
38 {
39 sbText.Append (info.Substring (0, 1) + "****");
40}
41}
42 else
43 {
44 if (info.Length> right && right> 0)
45 {
46 sbText.Append ("****" + info.Substring (info.Length-right));
47}
48 else
49 {
50 sbText.Append ("****" + info.Substring (info.Length-1));
51}
52}
53}
54 return sbText.ToString ();
55}
Method 1.2: The number of * in the middle is fixed
1 /// <summary>
2 /// Hide sensitive information
3 /// </ summary>
4 /// <param name = "info"> information entity </ param>
5 /// <param name = "left"> the number of characters left to reserve </ param>
6 /// <param name = "right"> Number of characters reserved on the right </ param>
7 /// <param name = "basedOnLeft"> when the length is abnormal, whether to display the left
8 /// <code> true </ code> shows the left, <code> false </ code> shows the right
9 /// <returns> </ returns>
10 public static string HideSensitiveInfo1 (string info, int left, int right, bool basedOnLeft = true)
11 {
12 if (String.IsNullOrEmpty (info))
13 {
14 return "";
15}
16 StringBuilder sbText = new StringBuilder ();
17 int hiddenCharCount = info.Length-left-right;
18 if (hiddenCharCount> 0)
19 {
20 string prefix = info.Substring (0, left), suffix = info.Substring (info.Length-right);
21 sbText.Append (prefix);
22 sbText.Append ("****");
23 sbText.Append (suffix);
twenty four }
25 else
26 {
27 if (basedOnLeft)
28 {
29 if (info.Length> left && left> 0)
30 {
31 sbText.Append (info.Substring (0, left) + "****");
32}
33 else
34 {
35 sbText.Append (info.Substring (0, 1) + "****");
36}
37}
38 else
39 {
40 if (info.Length> right && right> 0)
41 {
42 sbText.Append ("****" + info.Substring (info.Length-right));
43}
44 else
45 {
46 sbText.Append ("****" + info.Substring (info.Length-1));
47}
48}
49}
50 return sbText.ToString ();
51}
Method 2: The number of "*" is fixed, set to 4, according to the ratio of the total length of the information, the default is 1/3
1 /// <summary>
2 /// Hide sensitive information
3 /// </ summary>
4 /// <param name = "info"> Information </ param>
5 /// <param name = "sublen"> the ratio of the total length of information to the left substring (or right substring) </ param>
6 /// <param name = "basedOnLeft"> when the length is abnormal, whether to display the left side, the default is true, the default display is the left side
7 /// <code> true </ code> shows the left, <code> false </ code> shows the right
8 /// <returns> </ returns>
9 public static string HideSensitiveInfo (string info, int sublen = 3, bool basedOnLeft = true)
10 {
11 if (String.IsNullOrEmpty (info))
12 {
13 return "";
14}
15 if (sublen <= 1)
16 {
17 sublen = 3;
18}
19 int subLength = info.Length / sublen;
20 if (subLength> 0 && info.Length> (subLength * 2))
twenty one {
22 string prefix = info.Substring (0, subLength), suffix = info.Substring (info.Length-subLength);
23 return prefix + "****" + suffix;
twenty four }
25 else
26 {
27 if (basedOnLeft)
28 {
29 string prefix = subLength> 0? Info.Substring (0, subLength): info.Substring (0, 1);
30 return prefix + "****";
31}
32 else
33 {
34 string suffix = subLength> 0? Info.Substring (info.Length-subLength): info.Substring (info.Length-1);
35 return "****" + suffix;
36}
37}
38}
Extension phone number 1
1 /// <summary>
2 /// Hide phone number details
3 /// </ summary>
4 /// <param name = "phone"> Mobile phone number </ param>
5 /// <param name = "left"> number of characters left reserved </ param>
6 /// <param name = "right"> Number of characters reserved on the right </ param>
7 /// <returns> </ returns>
8 public static string HideTelDetails (string phone, int left = 3, int right = 4)
9 {
10 return HideSensitiveInfo (phone, left, right);
11}
The test results are as follows:
Phone number 2
1 /// <summary>
2 /// Hide phone number details
3 /// </ summary>
4 /// <param name = "phone"> Mobile phone number </ param>
5 /// <param name = "left"> the number of characters left reserved </ param>
6 /// <param name = "right"> Number of characters reserved on the right </ param>
7 /// <returns> </ returns>
8 public static string HideTelDetails (string phone, int left = 3, int right = 4)
9 {
10 return HideSensitiveInfo1 (phone, left, right);
11}
The test results are as follows:
Email address
1 /// <summary>
2 /// Hide right click details
3 /// </ summary>
4 /// <param name = "email"> mail address </ param>
5 /// <param name = "left"> the number of characters reserved in the message header, the default value is set to 3 </ param>
6 /// <returns> </ returns>
7 public static string HideEmailDetails (string email, int left = 3)
8 {
9 if (String.IsNullOrEmpty (email))
10 {
11 return "";
12}
13 if (System.Text.RegularExpressions.Regex.IsMatch (email, @ "\ w + ([-+.] \ W +) * @ \ w + ([-.] \ W +) * \. \ W + ([-.] \ w +) * ")) // If it is an email address
14 {
15 int suffixLen = email.Length-email.LastIndexOf ('@');
16 return HideSensitiveInfo (email, left, suffixLen, false);
17}
18 else
19 {
20 return HideSensitiveInfo (email);
twenty one }
twenty two }