身份证,银行卡,姓名用*号隐藏中间数字

public class StringHelper
{
    /// 
    /// 将传入的字符串中间部分字符替换成特殊字符
    /// 
    /// 需要替换的字符串
    /// 前保留长度
    /// 尾保留长度
    /// 用于替换的特殊字符
    /// 被特殊字符替换的字符串
    public static string ReplaceWithSpecialChar(string value, int startLen = 4, int endLen = 4, char specialChar = '*')
    {
        try
        {
            if (string.IsNullOrEmpty(value)) return string.Empty;

            if (value.Length > (startLen + endLen))
            {
                int lenth = value.Length - startLen - endLen;

                string replaceStr = value.Substring(startLen, lenth);

                string specialStr = string.Empty;

                for (int i = 0; i < replaceStr.Length; i++)
                {
                    specialStr += specialChar;
                }
                value = value.Substring(0, startLen) + specialStr + value.Substring(value.Length - endLen);
            }
        }
        catch (Exception)
        {
            throw;
        }
        return value;
    }
}

你可能感兴趣的:(ASP.NET,c#)