Stringbuilder,它内部如何工作? [英] Stringbuilder, how does it internally work ?

查看:63
本文介绍了Stringbuilder,它内部如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道如何使用StringBuilder,据说每次修改它都不会创建它的新副本

添加或删除文本的内容。

但是,我想知道它是如何在内部做的那样?

我打算用一个字符串构建器来保存大量的

的文本,有几个megs o大小,稍后将在固定的偏移量上读取

,所以我需要知道这是否合适

。 br />
谢谢,

解决方案

< an ******* @ discussion.microsoft.com>写道:

我知道如何使用StringBuilder,据说每次通过添加或删除文本修改内容时都不会创建它的新副本。但是,我想知道它是如何在内部做到的?
我打算使用一个字符串构建器来保存大量的文本,有几个大小,以后再阅读
在固定偏移量上,所以我需要知道它是否适合它。




* exact *细节非常棘手 - 我相信它内部有一个

字符串,它会更改,当需要

时创建一个更大容量的新字符串。


* basic *然而,细节是它有效地像一个

ArrayList但是对于字符。如果您了解ArrayList的工作原理,那么

应该让您对StringBuilder有一个良好的感觉。


在您的情况下,如果您正在创建StringBuilder,附加了很多

文本,然后*只*从中读取,我会在开始阅读之前将其转换为字符串

(使用ToString)从中。这将使

代码更易于理解。 (大多数开发人员都知道

从各种方式读取字符串,但不是从

StringBuilder中读取。)


- -

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要给我发邮件


基本上,StringBuilder在内部保存一个字节数组(我是猜测,但是它不能保持搅拌,因为字符串是不可变的,所以它可以

只存储字符串的字节表示)。当容量超过
时,则会分配一个新的缓冲区。


如果你知道你肯定会在预先分配内部使用的缓冲区。基本上,将

Capacity属性设置为高于平均值b / b
所需的值。这样,在大多数情况下,内部缓冲区将分配给你需要的



希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

< an ******* @ discussion.microsoft.com>在消息中写道

news:09 **************************** @ phx.gbl ... < blockquote class =post_quotes>
我知道如何使用StringBuilder,据说每次通过添加或删除文本来修改内容时都不会创建它的新副本。<但是,我想知道它是如何在内部做到的?
我打算用一个字符串构建器来保存大量的文本,有几个megs o大小,以后再阅读
在固定的偏移量上,所以我需要知道它是否适合它。

谢谢,


Jon,


内部不能使用字符串,因为.NET中的字符串不可变。

字符串。


除非当然是字符串,否则你不是指一个实际的

System.String实例,而是一个字符数组。

-

- Nicholas Paldino [.NET / C#MVP]

- MV * @ spam.guard.caspershouse.com


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

< an ******* @ discussion.microsoft.com>写道:

我知道如何使用StringBuilder,据说每次通过添加或删除文本修改内容时都不会创建它的新副本。但是,我想知道它是如何在内部做到的?
我打算使用一个字符串构建器来保存大量的文本,有几个大小,以后再阅读
在固定的偏移量上,所以我需要知道它是否适合它。



* exact *细节非常棘手 - 我相信它内部有一个
它改变的字符串,当它需要时创建一个更大容量的新字符串。

* basic *细节,但是,它实际上就像一个
ArrayList但是对于字符。如果您了解ArrayList的工作原理,那么
应该会让您对StringBuilder有一个良好的感觉。

在您的情况下,如果您正在创建StringBuilder,请附加很多
文本到它,然后*只*从它读取,我会在你开始阅读之前将它转换为字符串
(使用ToString)。这将使
代码更易于理解。 (大多数开发人员都知道
以各种方式从字符串中读取,但不是从StringBuilder中读取。)

-
Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ 〜双向飞碟
如果回复小组,请不要给我发邮件




I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.
Thanks,

解决方案

<an*******@discussions.microsoft.com> wrote:

I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.



The *exact* details are quite tricky - I believe it internally has a
string which it changes, creating a new string of larger capacity when
it needs to.

The *basic* details, however, are that it''s effectively like an
ArrayList but for chars. If you understand how ArrayList works, that
should give you a good feeling for StringBuilder.

In your case, if you''re creating the StringBuilder, appending a lot of
text to it, and then *only* reading from it, I''d convert it to a String
(using ToString) before you start reading from it. That will make the
code easier to understand for one thing. (Most developers know about
reading from a string in various ways, but not reading from a
StringBuilder.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Basically, the StringBuilder holds an array of bytes internally (I''m
guessing, but it can''t hold stirngs, since strings are immutable, so it can
just store the byte representation of the string). When the capacity is
exceeded, then a new buffer is allocated.

If you know that you are definitely going to have text of that size in
thye StringBuilder, then for the best performance, you might want to
pre-allocate the buffer that is used internally. Basically, set the
Capacity property to a value that is above what you are going to need on
average. This way, in most cases, the internal buffer will be allocated to
hold what you need.

Hope this helps.

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

<an*******@discussions.microsoft.com> wrote in message
news:09****************************@phx.gbl...


I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.
Thanks,



Jon,

It can''t use a string internally, because of the immutable nature of
strings in .NET.

Unless of course by string, you don''t mean an actual instance of
System.String, but rather, an array of characters.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

<an*******@discussions.microsoft.com> wrote:

I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.



The *exact* details are quite tricky - I believe it internally has a
string which it changes, creating a new string of larger capacity when
it needs to.

The *basic* details, however, are that it''s effectively like an
ArrayList but for chars. If you understand how ArrayList works, that
should give you a good feeling for StringBuilder.

In your case, if you''re creating the StringBuilder, appending a lot of
text to it, and then *only* reading from it, I''d convert it to a String
(using ToString) before you start reading from it. That will make the
code easier to understand for one thing. (Most developers know about
reading from a string in various ways, but not reading from a
StringBuilder.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于Stringbuilder,它内部如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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