/// <summary>
        /// 设置字符串将中间部份隐藏
        /// </summary>
        /// <param name="str">受控字符串</param>
        /// <param name="length">对比字符串长度(最少9位)</param>
        /// <returns>string</returns>
        public string HideCententStr(string str,int length)
        {
            string stateStr = string.Empty, endStr = string.Empty;
            StringBuilder sb = new StringBuilder();
            if (str.Length > length)
            {
                stateStr = str.Substring(0, 4);
                endStr = str.Substring((str.Length - 4), 4);
                return sb.Append(stateStr).Append(str.Replace(str, "****")).Append(endStr).ToString();
            }
            return str;
        }运行结果:
您的问题****将返还您.
您的问题得到解

解决方案 »

  1.   

    如果是一个列表同时用这个 方法 会出现参差不齐
     如:
    反复反复反复
    ffffff
    反反复复ff
    %……&……
    这钟效果 还是用字节好点
      

  2.   

    我来帮您引一下, Extend Method
     public static class StringExtend
        {
            public static string HideCenter(this string source, int minLength = 9, string hideString = "****")
            {
                if (!string.IsNullOrWhiteSpace(source) && 
                    source.Length > minLength && 
                    source.Length >= hideString.Length+2)
                {
                    int index = (source.Length - hideString.Length) / 2;
                    return source.Remove(index, source.Length - index * 2).Insert(index, hideString);
                }
                else
                {
                    return source;
                }
            }
        }
    测试代码
                string abc = "teststringteststring";
                string ret = abc.HideCenter();
                ret = abc.HideCenter(5, "###");
                ret = abc.HideCenter(6, "*&^");
      

  3.   


            public string HideCenterString(string source, int length)
            {
                string strCenter = "*";
                if (length >= source.Length)
                {
                    return strCenter.PadLeft(length - 1, '*');
                }
                else
                {
                    int start = (source.Length - length) / 2;
                    return source.Remove(start, length).Insert(start, strCenter.PadLeft(length - 1, '*'));            }
            }