From 080dcdb310df7f42b9485f26b6921bef48d3bece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=87=92=E5=BE=97=E5=8B=A4=E5=BF=AB?= Date: Tue, 21 Apr 2020 12:25:35 +0800 Subject: [PATCH] mask --- Masuit.Tools/Extensions.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Masuit.Tools/Extensions.cs b/Masuit.Tools/Extensions.cs index f18a947..c72ac36 100644 --- a/Masuit.Tools/Extensions.cs +++ b/Masuit.Tools/Extensions.cs @@ -631,7 +631,7 @@ namespace Masuit.Tools /// public static bool Contains(this string s, IEnumerable keys, bool ignoreCase = true) { - if (!keys.Any()) + if (!keys.Any() || string.IsNullOrEmpty(s)) { return false; } @@ -867,7 +867,7 @@ namespace Masuit.Tools } catch { - return default(T); + return default; } } @@ -1310,10 +1310,14 @@ namespace Masuit.Tools return s; } s = s.Trim(); - string masks = new string(new[] { mask, mask, mask, mask }); + string masks = mask.ToString().PadLeft(4, mask); return s.Length switch { - _ when s.Length >= 7 => Regex.Replace(s, @"(\w{3})\w*(\w{4})", $"$1{masks}$2"), + _ when s.Length >= 11 => Regex.Replace(s, @"(\w{3})\w*(\w{4})", $"$1{masks}$2"), + _ when s.Length == 10 => Regex.Replace(s, @"(\w{3})\w*(\w{3})", $"$1{masks}$2"), + _ when s.Length == 9 => Regex.Replace(s, @"(\w{2})\w*(\w{3})", $"$1{masks}$2"), + _ when s.Length == 8 => Regex.Replace(s, @"(\w{2})\w*(\w{2})", $"$1{masks}$2"), + _ when s.Length == 7 => Regex.Replace(s, @"(\w{1})\w*(\w{2})", $"$1{masks}$2"), _ when s.Length >= 2 && s.Length < 7 => Regex.Replace(s, @"(\w{1})\w*(\w{1})", $"$1{masks}$2"), _ => s + masks };