zoukankan      html  css  js  c++  java
  • HString类替代系统string,内部采用StringBuilder拼接,减少string+操作时对象的创建。

    如题,HString类结构如下:

          /// <summary>
          /// HString类替代系统string,
        /// 内部采用StringBuilder拼接,减少string+操作时对象的创建。
        /// </summary>
        public class HString
        {
            /// <summary>
            /// 内部StringBuilder变量
            /// </summary>
            private StringBuilder builder = null;
    
            /// <summary>
            /// 默认无参数构造函数
            /// </summary>
            public HString()
            {
                if (builder == null)
                {
                    builder = new StringBuilder();
                }
            }
    
            /// <summary>
            /// 带string参数的构造函数
            /// </summary>
            public HString(string v)
                : this()
            {
                builder.Append(v);
            }
    
            /// <summary>
            /// 将string对象隐式转换为HString对象
            /// </summary>
            public static implicit operator HString(string v)
            {
                return new HString(v);
            }
    
            /// <summary>
            /// 将HString对象隐式转换为字符串
            /// </summary>
            public static implicit operator string(HString v)
            {
                return v.ToString();
            }
    
            /// <summary>
            /// HString + string方式
            /// </summary>
            public static HString operator +(HString hs, string v)
            {
                if (string.IsNullOrEmpty(v) == false)
                {
                    hs.builder.Append(v);
                }
                return hs;
            }
            /// <summary>
            /// string + HString方式
            /// </summary>
            public static HString operator +(string v, HString hs)
            {
                if (string.IsNullOrEmpty(v) == false)
                {
                    hs.builder.Insert(0, v);
                }
                return hs;
            }
    
            /// <summary>
            /// object + HString方式
            /// </summary>
            public static HString operator +(object v, HString hs)
            {
                if (v != null)
                {
                    hs.builder.Insert(0, v);
                }
                return hs;
            }
    
            /// <summary>
            /// HString + object方式
            /// </summary>
            public static HString operator +(HString hs, object v)
            {
                if (v != null)
                {
                    hs.builder.Append(v);
                }
                return hs;
            }
    
            /// <summary>
            /// 追加格式化字符串
            /// </summary>
            public void AppendFormat(string format, params object[] args)
            {
                if (args != null && args.Length > 0)
                {
                    builder.AppendFormat(format, args);
                }
            }
    
            /// <summary>
            /// 重载对象的默认ToString方法输出拼接字符
            /// </summary>
            public override string ToString()
            {
                return builder.ToString();
            }
        }


    使用代码:

                HString str = "test";
                HString str = new HString();
                HString str = new HString("test");
    str += "xxx";
    string res = str;
    Console.WriteLine(res);
    Console.WriteLine(str);
  • 相关阅读:
    (转载)李开复:我在硅谷看到的最前沿科技趋势
    1019. 数字黑洞 (20)
    1018. 锤子剪刀布 (20)
    1017. A除以B (20)
    1016. 部分A+B (15)
    1015. 德才论 (25)
    1013. 数素数 (20)
    1014. 福尔摩斯的约会 (20)
    1012. 数字分类 (20)
    1011. A+B和C (15)
  • 原文地址:https://www.cnblogs.com/strayromeo/p/3054503.html
Copyright © 2011-2022 走看看