字符串在哪些地方比 StringBuilder 更有用?

发布于 2024-09-05 20:29:28 字数 114 浏览 8 评论 0原文

关于字符串和字符串生成器之间的差异已经提出了很多问题,大多数人认为字符串生成器比字符串更快。我很想知道字符串生成器是否太好了,那么为什么字符串会存在呢?此外,有人可以给我一个例子,其中字符串比字符串生成器更有用吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦明 2024-09-12 20:29:28

正如其名称所示,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.

守望孤独 2024-09-12 20:29:28

通过大量循环或迭代构造和修改字符串时,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

浸婚纱 2024-09-12 20:29:28

这并不是哪种情况更有用...

String 是一个 String - 一个或多个彼此相邻的字符。如果您想以某种方式更改字符串,它只会创建更多字符串,因为它们是不可变

StringBuilder 是一个创建字符串的类。它提供了一种构造它们的方法,而无需在内存中创建大量冗余字符串。最终结果始终是一个String

不要这样做

string s = "my string";
s += " is now a little longer";

s = s + " is now longer again";

这会在内存中创建 5 个字符串(实际上,见下文)。

这样做:

StringBuilder sb = new StringBuilder();
sb.Append("my string");
sb.Append(" is now a little longer");
sb.Append(" is now longer again");
string s = sb.ToString();

这将在内存中创建 1 字符串(再次参见下文)。

您可以这样做:

string s = "It is now " + DateTime.Now + ".";

这只会在内存中创建 1 字符串。

顺便说一句,创建 StringBuilder 无论如何都会占用一定量的内存。粗略的经验法则是:

  • 如果要在循环中连接字符串,请始终使用 StringBuilder
  • 如果您连接字符串超过 4 次,请使用 StringBuilder

It's not a case of which is more useful...

A String is a String - 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 a String.

Don't do this

string s = "my string";
s += " is now a little longer";

or

s = s + " is now longer again";

That would create 5 strings in memory (in reality, see below).

Do this:

StringBuilder sb = new StringBuilder();
sb.Append("my string");
sb.Append(" is now a little longer");
sb.Append(" is now longer again");
string s = sb.ToString();

That would create 1 string in memory (again, see below).

You can do this:

string s = "It is now " + DateTime.Now + ".";

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:

  • Always use a StringBuilder if you're concatenating strings in a loop.
  • Use a StringBuilder if you're concatenating a string more than 4 times.
会傲 2024-09-12 20:29:28

你真的会在这里使用字符串生成器吗?

Console.WriteLine("MyString");

当您需要进行大量串联来构建字符串时,StringBuilder 非常有用。

Jon Skeet 在这方面有一篇很棒的文章:http://www.yoda.arachsys。 com/csharp/stringbuilder.html

查看以“所以我应该到处使用 StringBuilder,对吗?”开头的部分。

他写道:

这之间的重要区别
例子和前一个是
我们可以轻松呈现所有字符串
需要连接在一起
在对 String.Concat 的一次调用中。那
意味着没有中间字符串
需要。 StringBuilder 的效率很高
第一个例子,因为它充当
中间结果的容器
无需复制该结果
每次 - 当没有
无论如何,中间结果没有
优势。


Would you really use a string builder here?

Console.WriteLine("MyString");

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:

The important difference between this
example and the previous one is that
we can easily present all the strings
which need to be concatenated together
in one call to String.Concat. That
means that no intermediate strings are
needed. StringBuilder is efficient in
the first example because it acts as a
container for the intermediate result
without having to copy that result
each time - when there's no
intermediate result anyway, it has no
advantage.


感情废物 2024-09-12 20:29:28

当您需要更改值时,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.

伏妖词 2024-09-12 20:29:28

简而言之,这是可变不可变之间的权衡,

请参阅:StringBuilder 与 String

In brief, it's a trade-off between mutable and immutable

See this:StringBuilder vs String

昔梦 2024-09-12 20:29:28

就创建字符串而言,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.

嘿看小鸭子会跑 2024-09-12 20:29:28

字符串很好,因为它是不可变的。不可变是好的,函数式语言已经证明了这一点。

因为如果您有一个(引用)字符串“This is a string”,您可以确定它将永远具有相同的值。虽然 StringBuilder 的值可能会更改(通过方法调用、另一个线程等)。

但是,有时您需要原始、纯粹的速度和内存使用优化。在这种情况下,您可以使用字符串生成器。

一个例子:

 var a = "This is a string";
 var b = a;
 a += ". And some part is added";   // a now references another string in memory

 var aSB = new StringBuilder("This is a string");
 var bSB = aSB
 aSB.Append(". And some part is added"); // aSB and bSB still reference the same memory spot

 Console.WriteLine(b);   // ==> This is a string
 Console.WriteLine(bSB); // ==> This is a string. And some part is added

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:

 var a = "This is a string";
 var b = a;
 a += ". And some part is added";   // a now references another string in memory

 var aSB = new StringBuilder("This is a string");
 var bSB = aSB
 aSB.Append(". And some part is added"); // aSB and bSB still reference the same memory spot

 Console.WriteLine(b);   // ==> This is a string
 Console.WriteLine(bSB); // ==> This is a string. And some part is added
等待圉鍢 2024-09-12 20:29:28

有关使用哪种方法的分析位于:高效连接字符串

本质上,(尽管阅读链接以进行完整分析),在重要循环中连接时使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文