Stringbuilder vs Strings [英] Stringbuilder vs Strings

查看:64
本文介绍了Stringbuilder vs Strings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人建议我为什么StringBuilder类比Strings更好?

-

永远不要说,放弃

解决方案

5月9日下午2:39,Dara P< D ... @ discussion.microsoft.comwrote:


有人可以建议我为什么StringBuilder类比Strings更好?



请参阅 http://pobox.com/~skeet/csharp/stringbuilder.html

Jon


< blockquote> 5月9日上午9:39,Dara P< D ... @ discussion.microsoft.comwrote:


有人可以建议我为什么是StringBuilder上课比Strings好吗?



它不是更好,它有不同的用途。使用StringBuilder

你有很多字符串要连接,因为SB执行

连接和其他字符串操作操作比

使用更快标准字符串类。


应该说你应该使用StringBuilder连接

你还不知道你要连接多少个字符串。

最好的例子就是当你有N个字符串并且需要将它们一起追加到

时。


如果你有一个已知数量的字符串,那么使用串联

运算符就可以了,因为它会编译为调用静态

Concat方法字符串类,它非常快(它预先分配

返回缓冲区,然后将字符串的内存副本放入返回缓冲区中的适当位置

) 。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" ;安迪" < an *** @ med-associates.comwrote in message

news:11 ********************* @ e65g2000hsc.googlegro ups.com ...


5月9日上午9:39,Dara P< D ... @ discussion.microsoft.comwrote:


>有人可以建议我为什么StringBuilder类优于
字符串?



它不是更好,它有不同的用途。使用StringBuilder

你有很多字符串要连接,因为SB执行

连接和其他字符串操作操作比

使用更快标准字符串类。



Can some one suggest me as why StringBuilder class is better than Strings?
--
Never say, Give up

解决方案

On May 9, 2:39 pm, Dara P <D...@discussions.microsoft.comwrote:

Can some one suggest me as why StringBuilder class is better than Strings?

See http://pobox.com/~skeet/csharp/stringbuilder.html

Jon


On May 9, 9:39 am, Dara P <D...@discussions.microsoft.comwrote:

Can some one suggest me as why StringBuilder class is better than Strings?

Its not better, it serves a different purpose. Use StringBuilder when
you have a lot of strings to concatinate, as the SB performs
concatination and other string manipulation operations faster than
using a standard string class.


It should be said that you should use StringBuilder to concatenate when
you don''t exactly know how many strings you are going to concatenate. The
best example is when you have N number of strings and you need to append it
all together in a loop.

If you have a known number of strings, then using the concatenation
operator will work just fine, as it will compile to a call to the static
Concat method on the string class, which is very fast (it pre-allocates the
return buffer, and then does a memory copy of the strings into the
appropriate locations in the return buffer).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy" <an***@med-associates.comwrote in message
news:11*********************@e65g2000hsc.googlegro ups.com...

On May 9, 9:39 am, Dara P <D...@discussions.microsoft.comwrote:

>Can some one suggest me as why StringBuilder class is better than
Strings?


Its not better, it serves a different purpose. Use StringBuilder when
you have a lot of strings to concatinate, as the SB performs
concatination and other string manipulation operations faster than
using a standard string class.



这篇关于Stringbuilder vs Strings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆