码迷,mamicode.com
首页 > 其他好文 > 详细

Stringbuilder用法

时间:2015-05-25 18:04:42      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:

Stringbuilder搜索类是直接用于字符串操作的类,打个比方把
(1)string aa="123456";
(2)aa+="789";


(3)StringBuilder text=new StringBuilder("123456",12);
(4)text.Append("789");
如果你输出aa,和text 你会发现他们的输出内容是一样的。
但是aa的操作过程实际上是:首先在内存中分配一个地址空间,空间大小是6。
然后执行  aa+="789";的操作,该过程是连接字符串,“123456”和“789”并且在内存中重新分配地址。把aa 的内存地址指向 “123456789”的内存地址。

也就是说在内存中实际上是有两个空间北分配,第一的内存空间,在后来是由C#的垃圾处理机制来自动处理掉,

如果我们用3 4 句的程序来实现这个过程,那么他是没有再次分配内存空间的,
他就是在text的内存空间里进行了操作。这里要说明下StringBuilder在生命变量的过程中是可以我们自己来分配他的大小的,如果实际的内容超出内存空间,
他会自动翻倍。

通过上面的例子,我们可以知道 StringBuilder的优越性是在:
第一:他不需要每次都去分配内存空间。所以系统就没有必要去处理垃圾;
第二:当我们需要多次的对一个字符串进行多次操作的时候,他的效率要远远  高  与string

Stringbuilder一般用来拼接sql语句

                string txtmonth = Request.Form["jqxMonth"].ToString();
                string txtyear = Request.Form["txtYear"].ToString();
                string txtRegion = Request.Form["jqxRegion"].ToString();
                StringBuilder StrWhere = new StringBuilder();
                StrWhere.Append(" 1 = 1 ");
                if (txtRegion != "")
                {
                    StrWhere.Append(" AND Region=‘" + txtRegion + "‘");
                }
                if (txtyear != "")
                {
                    if (txtmonth != "")
                        StrWhere.Append(" and Year_Month=‘" + txtyear + "-" + txtmonth + "‘");
                    else
                        StrWhere.Append(" and Year_Month like ‘%" + txtyear + "%‘");
                }
                if (txtApplication != "")
                {
                    StrWhere.Append(" and isnull(Region,‘‘)+isnull(Ori_Currency,‘‘)+isnull(Currency1,‘‘)+isnull(Currency2,‘‘)+isnull(Currency3,‘‘)+isnull(Currency4,‘‘)+isnull(Currency5,‘‘)+isnull(convert(nvarchar(10),Year_Month,120),‘‘)+isnull(cast(Fxrate_CNY as varchar),‘‘)+isnull(cast(Fxrate_EUR as varchar),‘‘)+isnull(cast(Fxrate_HKD as varchar),‘‘)+isnull(cast(Fxrate_TWD as varchar),‘‘)+isnull(Status,‘‘) LIKE ‘%" + txtApplication + "%‘");
                }
                StrWhere.Append(" and IsDel=0");

 

Stringbuilder用法

标签:

原文地址:http://www.cnblogs.com/rookie-26/p/4528248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!