实现手机号码、身份证号码、邮件等部分星号等特殊符号遮盖功能,用于隐私保护,效果如下 136****0000 322300*******32432143
同上效果图(24TT抽奖软件设置与运行效果图):
1 /// <summary> 2 /// 通过长度生成遮盖字段如**** 3 /// </summary> 4 /// <param name="program_StarLen">长度如4</param> 5 /// <param name="program_StarChar">遮盖符号如*</param> 6 /// <returns></returns> 7 public static string StarCharShow(int program_StarLen, string program_StarChar) 8 { 9 string program_StarCharShow = ""; 10 for (int i = 0; i < program_StarLen; i++) 11 { 12 program_StarCharShow = program_StarCharShow + program_StarChar; 13 } 14 return program_StarCharShow; 15 } 16 /// <summary> 17 /// 特殊符号遮盖 18 /// </summary> 19 /// <param name="str">拟遮盖字符全字段</param> 20 /// <param name="program_starBegin">开始位</param> 21 /// <param name="program_starEnd">结束位</param> 22 /// <param name="program_StarCharShow">遮盖组合符号</param> 23 /// <returns></returns> 24 public static string StrReplaceByStar(string str, int program_starBegin, int program_starEnd, string program_StarChar) 25 { 26 string NewStr = ""; 27 string a = ""; 28 string b = ""; 29 int starLen=program_starEnd-program_starBegin+1; 30 if (starLen <= 0) 31 { 32 return str; 33 } 34 string program_StarCharShow=StarCharShow(starLen, program_StarChar); 35 if (str.Length <= program_starEnd) 36 { 37 if (str.Length < program_starBegin) 38 { 39 a = str; 40 } 41 else 42 { 43 a = str.Substring(0, program_starBegin - 1); 44 } 45 } 46 else 47 { 48 a = str.Substring(0, program_starBegin - 1); 49 b = str.Substring(program_starEnd, str.Length - program_starEnd); 50 } 51 NewStr = a + program_StarCharShow + b; 52 return NewStr; 53 } 54 /// <summary> 55 /// 特殊符号遮盖 56 /// </summary> 57 /// <param name="str">拟遮盖字符全字段</param> 58 /// <param name="program_starBegin">开始位</param> 59 /// <param name="program_starEnd">结束位</param> 60 /// <param name="program_StarChar">遮盖符号单个</param> 61 /// <returns></returns> 62 public static string StrReplaceByStars(string str, int program_starBegin, int program_starEnd, string program_StarCharShow) 63 { 64 string NewStr = ""; 65 string a = ""; 66 string b = ""; 67 if (str.Length <= program_starEnd) 68 { 69 if (str.Length < program_starBegin) 70 { 71 a = str; 72 } 73 else 74 { 75 a = str.Substring(0, program_starBegin - 1); 76 } 77 } 78 else 79 { 80 a = str.Substring(0, program_starBegin - 1); 81 b = str.Substring(program_starEnd, str.Length - program_starEnd); 82 } 83 NewStr = a + program_StarCharShow + b; 84 return NewStr; 85 }
时间: 2024-10-11 13:27:15