From 74f5471f2f833e8727f0d69fabd61272dfd08023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=87=92=E5=BE=97=E5=8B=A4=E5=BF=AB?= Date: Wed, 15 Apr 2020 17:15:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7=E9=82=AE=E7=AE=B1?= =?UTF-8?q?=E7=AD=89mask?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Masuit.Tools.Core/Masuit.Tools.Core.csproj | 2 +- Masuit.Tools/Extensions.cs | 35 ++++++++++++++++++++- Masuit.Tools/Properties/AssemblyInfo.cs | Bin 2078 -> 2078 bytes Masuit.Tools/package.nuspec | 2 +- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Masuit.Tools.Core/Masuit.Tools.Core.csproj b/Masuit.Tools.Core/Masuit.Tools.Core.csproj index 6abd72f..94fd1f4 100644 --- a/Masuit.Tools.Core/Masuit.Tools.Core.csproj +++ b/Masuit.Tools.Core/Masuit.Tools.Core.csproj @@ -2,7 +2,7 @@ netstandard2.1 - 2.2.8.6 + 2.2.8.7 懒得勤快 masuit.com 包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。 diff --git a/Masuit.Tools/Extensions.cs b/Masuit.Tools/Extensions.cs index 2a7ba36..a168183 100644 --- a/Masuit.Tools/Extensions.cs +++ b/Masuit.Tools/Extensions.cs @@ -1280,10 +1280,43 @@ namespace Masuit.Tools /// public static void RemoveWhere(this ICollection @this, Func @where) { - foreach (var obj in @this.Where(@where).ToList()) + foreach (var obj in @this.Where(where).ToList()) { @this.Remove(obj); } } + + /// + /// 字符串掩码 + /// + /// 字符串 + /// 掩码符 + /// + public static string Mask(this string s, char mask = '*') + { + if (string.IsNullOrWhiteSpace(s?.Trim())) + { + return s; + } + s = s.Trim(); + string masks = new string(new[] { mask, mask, mask, mask }); + return s.Length switch + { + _ when s.Length > 4 => Regex.Replace(s, "(\\w{3})\\w+(\\w{4})", $"$1{masks}$2"), + _ when s.Length >= 2 && s.Length <= 4 => Regex.Replace(s, "(\\w{1})\\w*(\\w{1})", $"$1{masks}$2"), + _ => s + masks + }; + } + + /// + /// 邮箱掩码 + /// + /// 邮箱 + /// 掩码 + /// + public static string MaskEmail(this string s, char mask = '*') + { + return !MatchEmail(s) ? s : s.Replace(s.Substring(0, s.LastIndexOf("@")), Mask(s.Substring(0, s.LastIndexOf("@")), mask)); + } } } \ No newline at end of file diff --git a/Masuit.Tools/Properties/AssemblyInfo.cs b/Masuit.Tools/Properties/AssemblyInfo.cs index e6b2e3fe2dee61830d3721247f5d0c64768fca62..f18792de71df9815230977ce81166c17d14760d7 100644 GIT binary patch delta 33 kcmbOyFi&6u3p=CvWLEZIAi0rUaq Masuit.Tools.Net - 2.2.8.5 + 2.2.8.7 Masuit.Tools masuit.com