近期.NET学习及项目开发整理 -StringBuilder 的使用

StringBuilder 的使用

String 对象是不可改变的。每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间。在需要对字符串执行重复修改的情况下,与创建新的 String 对象相关的系统开销可能会非常昂贵。如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。例如,当在一个循环中将许多字符串连接在一起时,使用 StringBuilder 类可以提升性能。

StringBuilder buffer = new StringBuilder();
            buffer.Append("select * from Users ");
            buffer.Append(" where UserId=@UserId");
            SqlParameter[] parameters = {
                    new SqlParameter("@UserId", SqlDbType.Int,4)};
            parameters[0].Value = userId;

            SqlDbHelper db = new SqlDbHelper();
            return db.ExecuteDataTable(buffer.ToString(), CommandType.Text, parameters);

 

可以使用读/写 Capacity 属性来设置对象的最大长度。下面的示例使用 Capacity 属性来定义对象的最大长度。

 

 

StringBuilder.Append

将信息追加到当前 StringBuilder 的结尾。

  1.  
    1.  
      1.  
        1.  
          1.  
            1. buffer.Append("update xjy_cust set ");
              buffer.Append("candh=0");        
              buffer.Append(" where cust_no=@cust_no");

StringBuilder.AppendFormat

用带格式文本替换字符串中传递的格式说明符。

static StringBuilder sb = new StringBuilder();

   public static void Main()
   {
       int var1 = 111;
       float var2 = 2.22F;
       string var3 = "abcd";
       object[] var4 = { 3, 4.4, 'X' };

       Console.WriteLine();
       Console.WriteLine("StringBuilder.AppendFormat method:");
       sb.AppendFormat("1) {0}", var1);
       Show(sb);
       sb.AppendFormat("2) {0}, {1}", var1, var2);
       Show(sb);
       sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3);
       Show(sb);
       sb.AppendFormat("4) {0}, {1}, {2}", var4);
       Show(sb);
       CultureInfo ci = new CultureInfo("es-ES", true);
       sb.AppendFormat(ci, "5) {0}", var2);
       Show(sb);
   }

   public static void Show(StringBuilder sbs)
   {
       Console.WriteLine(sbs.ToString());
       sb.Length = 0;
   }

StringBuilder.Insert

将字符串或对象插入到当前 StringBuilder 对象的指定索引处。

using System;
using System.Text;

class Sample
{
//                         index: 012345
    static string initialValue = "--[]--";
    static StringBuilder sb;

    public static void Main()
    {
    string      xyz       = "xyz";
    char[]      abc       = {'a', 'b', 'c'};
    char        star      = '*';
    Object     obj       = 0;

    bool        xBool     = true;
    byte        xByte     = 1;
    short       xInt16    = 2;
    int         xInt32    = 3;
    long        xInt64    = 4;
    Decimal     xDecimal  = 5;
    float       xSingle   = 6.6F;
    double      xDouble   = 7.7;

// The following types are not CLS-compliant.
    ushort      xUInt16   = 8;
    uint        xUInt32   = 9;
    ulong       xUInt64   = 10;
    sbyte       xSByte    = -11;
//
    Console.WriteLine("StringBuilder.Insert method");
    sb = new StringBuilder(initialValue);

    sb.Insert(3, xyz, 2);
    Show(1, sb);

    sb.Insert(3, xyz);
    Show(2, sb);

    sb.Insert(3, star);
    Show(3, sb);

    sb.Insert(3, abc);
    Show(4, sb);

    sb.Insert(3, abc, 1, 2);
    Show(5, sb);

    sb.Insert(3, xBool);     // True
    Show(6, sb);

    sb.Insert(3, obj);       // 0
    Show(7, sb);

    sb.Insert(3, xByte);     // 1
    Show(8, sb);

    sb.Insert(3, xInt16);    // 2
    Show(9, sb);

    sb.Insert(3, xInt32);    // 3
    Show(10, sb);

    sb.Insert(3, xInt64);    // 4
    Show(11, sb);

    sb.Insert(3, xDecimal);  // 5
    Show(12, sb);

    sb.Insert(3, xSingle);   // 6.6
    Show(13, sb);

    sb.Insert(3, xDouble);   // 7.7
    Show(14, sb);

// The following Insert methods are not CLS-compliant.
    sb.Insert(3, xUInt16);   // 8
    Show(15, sb);

    sb.Insert(3, xUInt32);   // 9
    Show(16, sb);

    sb.Insert(3, xUInt64);   // 10
    Show(17, sb);

    sb.Insert(3, xSByte);    // -11
    Show(18, sb);
//
    }

    public static void Show(int overloadNumber, StringBuilder sbs)
    {
    Console.WriteLine("{0,2:G} = {1}", overloadNumber, sbs.ToString());
    sb = new StringBuilder(initialValue);
    }
}
/*
This example produces the following results:

StringBuilder.Insert method
1 = --[xyzxyz]--
2 = --[xyz]--
3 = --[*]--
4 = --[abc]--
5 = --[bc]--
6 = --[True]--
7 = --[0]--
8 = --[1]--
9 = --[2]--
10 = --[3]--
11 = --[4]--
12 = --[5]--
13 = --[6.6]--
14 = --[7.7]--
15 = --[8]--
16 = --[9]--
17 = --[10]--
18 = --[-11]--

*/

StringBuilder.Remove

从当前 StringBuilder 对象中移除指定数量的字符。

public StringBuilder Remove (
    int startIndex,
    int length
)

StringBuilder.Replace

替换指定索引处的指定字符

public StringBuilder Replace (
    string oldValue,
    string newValue
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值