手机号邮箱等mask

This commit is contained in:
懒得勤快
2020-04-15 17:15:54 +08:00
parent 08b614d846
commit 74f5471f2f
4 changed files with 36 additions and 3 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>2.2.8.6</Version>
<Version>2.2.8.7</Version>
<Authors>懒得勤快</Authors>
<Company>masuit.com</Company>
<Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。
+34 -1
View File
@@ -1280,10 +1280,43 @@ namespace Masuit.Tools
/// <param name="where"></param>
public static void RemoveWhere<T>(this ICollection<T> @this, Func<T, bool> @where)
{
foreach (var obj in @this.Where(@where).ToList())
foreach (var obj in @this.Where(where).ToList())
{
@this.Remove(obj);
}
}
/// <summary>
/// 字符串掩码
/// </summary>
/// <param name="s">字符串</param>
/// <param name="mask">掩码符</param>
/// <returns></returns>
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
};
}
/// <summary>
/// 邮箱掩码
/// </summary>
/// <param name="s">邮箱</param>
/// <param name="mask">掩码</param>
/// <returns></returns>
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));
}
}
}
Binary file not shown.
+1 -1
View File
@@ -4,7 +4,7 @@
<!--*-->
<id>Masuit.Tools.Net</id>
<!--*-->
<version>2.2.8.5</version>
<version>2.2.8.7</version>
<title>Masuit.Tools</title>
<!--*-->
<authors>masuit.com</authors>