通用手机号、身份证号等隐藏显示方法

     /// <summary>
        /// 半隐藏显示,eg.传值:13112345678,4,4;结果:1311***5678。传值:13112345678,4,4,1,"&";结果:1311&5678。
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="beginShowStrLength">开始显示字符长度</param>
        /// <param name="endShowStrLength">结束显示字符串长度</param>
        /// <param name="replaceShowStrLength">替换后显示字符长度 默认:-1</param>
        /// <param name="replaceSymbol">替换后显示的字符,默认:*</param>
        /// <returns></returns>
        public static string Display(this string str, int beginShowStrLength, int endShowStrLength, int replaceShowStrLength = -1, string replaceSymbol = "*")
        {
            var length = str.Length - beginShowStrLength - endShowStrLength;
            replaceShowStrLength = replaceShowStrLength == -1 ? length : replaceShowStrLength;
            var stringBuilder = new StringBuilder();
            for (int i = 0; i < replaceShowStrLength; i++)
            {
                stringBuilder.Append(replaceSymbol);
            }
            var replaceShowStr = stringBuilder.ToString();
            return Regex.Replace(str, "(\\S{" + beginShowStrLength + "})\\S{" + length + "}(\\S{" + endShowStrLength + "})", "$1" + replaceShowStr + "$2");
        }

 

posted @ 2018-04-28 09:40  随学·笔记  阅读(896)  评论(0编辑  收藏  举报