执行结果 ----------------- 字符串加掩码 ----------------- 123*****89 执行代码 Console.WriteLine("----------------- 字符串加掩码 -----------------"); string str = "123456789"; Console.WriteLine(StringMask(str,3,2)); Conso
执行结果
----------------- 字符串加掩码 -----------------
123*****89
执行代码
Console.WriteLine("----------------- 字符串加掩码 -----------------");
string str = "123456789";
Console.WriteLine(StringMask(str,3,2));
Console.Read();
工具代码
/// <summary>
/// 字符串加掩码,字符串第一位的下标是0
/// </summary>
/// <param name="ValueString">值</param>
/// <param name="StartReserve">起始保留字节数</param>
/// <param name="EndReserve">尾部保留字节数</param>
/// <returns></returns>
public static string StringMask(string ValueString, int StartReserve, int EndReserve = 0)
{
if (!string.IsNullOrEmpty(ValueString))
{
int len = ValueString.Length;
if (EndReserve >= len)
EndReserve = 3;
if (EndReserve < 0|| len<0)
EndReserve = 0;
return ValueString.Substring(0, StartReserve) + "*****" + ValueString.Substring(len - EndReserve);
}
return string.Empty;
}