字符串在哪些地方比 StringBuilder 更有用?
关于字符串和字符串生成器之间的差异已经提出了很多问题,大多数人认为字符串生成器比字符串更快。我很想知道字符串生成器是否太好了,那么为什么字符串会存在呢?此外,有人可以给我一个例子,其中字符串比字符串生成器更有用吗?
Lot of questions has been already asked about the differences between string and string builder and most of the people suggest that string builder is faster than string. I am curious to know if string builder is too good so why string is there? Moreover, can some body give me an example where string will be more usefull than string builder?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
正如其名称所示,StringBuilder 用于构建字符串,而字符串是不可变的字符值。当您想要传递字符数据时,请使用字符串;当您想要操作字符数据时,请使用 StringBuilder。
StringBuilder is, as its name suggested, is used to build strings, whereas strings are immutable character-values. Use strings when you want to pass around character data, use StringBuilder when you want to manipulate character data.
通过大量循环或迭代构造和修改字符串时,StringBuilder 是更好的选择。
简单的字符串操作(例如一些串联或简单地存储字符串值)更适合标准字符串对象。
类似的问题? 字符串与 StringBuilder
StringBuilder is a better option when constructing and modifying strings through heavy looping or iterations.
Simple string operations such as a few concatenations or simply storing a string value suits a standard string object far better.
Similar question? String vs. StringBuilder
这并不是哪种情况更有用...
String
是一个String
- 一个或多个彼此相邻的字符。如果您想以某种方式更改字符串,它只会创建更多字符串,因为它们是不可变。StringBuilder
是一个创建字符串的类。它提供了一种构造它们的方法,而无需在内存中创建大量冗余字符串。最终结果始终是一个String
。不要这样做
或
这会在内存中创建 5 个字符串(实际上,见下文)。
这样做:
这将在内存中创建 1 字符串(再次参见下文)。
您可以这样做:
这只会在内存中创建 1 字符串。
顺便说一句,创建 StringBuilder 无论如何都会占用一定量的内存。粗略的经验法则是:
StringBuilder
。StringBuilder
。It's not a case of which is more useful...
A
String
is aString
- one or more characters next to each other. If you want to change a string in someway, it will simply create more strings because they are immutable.A
StringBuilder
is a class which creates strings. It provides a means of constructing them without creating lots of reduntant strings in memory. The end result will always be aString
.Don't do this
or
That would create 5 strings in memory (in reality, see below).
Do this:
That would create 1 string in memory (again, see below).
You can do this:
This only creates 1 string in memory.
As a side-note, creating a
StringBuilder
does take a certain amount of memory anyway. As a rough rule of thumb:StringBuilder
if you're concatenating strings in a loop.StringBuilder
if you're concatenating a string more than 4 times.你真的会在这里使用字符串生成器吗?
当您需要进行大量串联来构建字符串时,StringBuilder 非常有用。
Jon Skeet 在这方面有一篇很棒的文章:http://www.yoda.arachsys。 com/csharp/stringbuilder.html
查看以“所以我应该到处使用 StringBuilder,对吗?”开头的部分。
他写道:
Would you really use a string builder here?
StringBuilder is great when you need to do large amounts of concatenation to build a string.
Jon Skeet has a great aricle on this here: http://www.yoda.arachsys.com/csharp/stringbuilder.html
Check out the section that starts "So I Should Use StringBuilder Everywhere, Right?"
He writes:
当您需要更改值时,StringBuilder 更好。否则,就没有必要了。
如果我需要在 10 个地方使用相同的字符串值,我将使用 String.如果我需要改变10次,StringBuilder。
StringBuilder is better when you need to change the value. Otherwise, there's no need for it.
If I need to use the same string value in 10 places, as it is, I'll use String. If I need to change it 10 times, StringBuilder.
简而言之,这是可变和不可变之间的权衡,
请参阅:StringBuilder 与 String
In brief, it's a trade-off between mutable and immutable
See this:StringBuilder vs String
就创建字符串而言,StringBuilder 并不比使用字符串快。
当您必须将多个字符串连接在一起(例如在循环中)时,StringBuilder 可以更快。当您必须将字符串生成器内容转换为字符串时,您将付出性能损失。
StringBuilder 相对于字符串的真正优势之一是您可以通过多种方法传递它并一路附加字符串。您不能通过传递字符串对象来做到这一点。因此,如果您有一个递归操作,您正在构建一个 JSON 对象或其他东西,您可以传递 StringBuilder 并附加值。
StringBuilder is not faster than using a string as far as just creating a string.
StringBuilder can be faster when you have to concatenate several strings together (like in loop). You are going to pay a performance penalty when you have to convert the string builder contents to a string.
One of the real advantages of StringBuilder over string is that you can pass it through several methods and append strings along the way. You can't do that by passing a string object. So if you have a recursive operation where you are building like a JSON object or something, you can pass the StringBuilder through and append the values.
字符串很好,因为它是不可变的。不可变是好的,函数式语言已经证明了这一点。
因为如果您有一个(引用)字符串“This is a string”,您可以确定它将永远具有相同的值。虽然 StringBuilder 的值可能会更改(通过方法调用、另一个线程等)。
但是,有时您需要原始、纯粹的速度和内存使用优化。在这种情况下,您可以使用字符串生成器。
一个例子:
A string is good because it is immutable. And that immutable is good, is something that functional languages are prove of.
Because if you have a (reference to) string "This is a string" you can be sure that it will have the same value forever. While the value of a StringBuilder might be changed (by a method call, another thread etc).
But, sometimes you need raw, pure speed and memory usage optimizations. In that case you use a stringbuilder.
An example:
有关使用哪种方法的分析位于:高效连接字符串。
本质上,(尽管阅读链接以进行完整分析),在重要循环中连接时使用 StringBuilder。
An analysis of which to use is provided at: Concatenating Strings Efficiently.
Essentially, (although read the link for full analysis), use
StringBuilder
when concatenating in a non-trivial loop.