随笔 - 11  文章 - 0 评论 - 2 trackbacks - 0

 
        /// <summary>
        /// 将传入的字符串中间部分字符替换成特殊字符
        /// </summary>
        /// <param name="value">需要替换的字符串</param>
        /// <param name="startLen">前保留长度</param>
        /// <param name="endLen">尾保留长度</param> 
        /// <param name="specialChar">特殊字符</param>
        /// <returns>被特殊字符替换的字符串</returns>
        public static string ReplaceWithSpecialChar(string value, int startLen = 3, int endLen = 4, char specialChar = '*')
        {
            try
            { 
                int lenth = value.Length - startLen - endLen;

                if (lenth == 0)
                    value = value.Substring(0, 1) + specialChar;
                if (lenth < 0)
                    value = specialChar.ToString();

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

                string specialStr = string.Empty;

                for (int i = 0; i < replaceStr.Length; i++)
                {
                    specialStr += specialChar;
                }

                value = value.Replace(replaceStr, specialStr);
            }
            catch (Exception)
            {
                return value;
            }

            return value;
        }

 

 

另外一个,隐藏手机号码中间4位

     /// <summary>
        /// 格式字符串-隐藏手机号中间数字
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string PhoneFormatString(string phone)
        {
            if (phone == null)
            {
                return string.Empty;
            } 
            return Regex.Replace(phone, "(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }

 

posted on 2018-10-24 15:05  沈超  阅读(...)  评论(...编辑  收藏