如何使用StringBuilder的明智 [英] How to use StringBuilder wisely

查看:100
本文介绍了如何使用StringBuilder的明智的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关于使用的StringBuilder 一流,有点糊涂:

I am little confused about using StringBuilder class, first:

一个字符串对象串联操作总是创建从现有的字符串,以及新数据的新对象。 A 的StringBuilder 对象维护一个缓冲区,以适应新的数据的串联。新的数据被追加到,如果房间可用缓冲区的结束;否则,一个新的,更大的缓冲区被分配,从原来的缓冲器的数据被复制到新的缓冲器,则新的数据被附加到新的缓冲区。

A String object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is appended to the end of the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, then the new data is appended to the new buffer.

但是,在为创建的StringBuilder 实例,以避免创建字符串的新的意义呢?这听起来像买卖一对一。

But where is the point of creating StringBuilder instance to avoid creating new one of String? It sounds like trading "one for one".

static void Main(string[] args)
{
    String foo = "123";
    using (StringBuilder sb = new StringBuilder(foo)) // also sb isn't disposable, so there will be error
    {
        sb.Append("456");
        foo = sb.ToString();
    }

    Console.WriteLine(foo);
    Console.ReadKey();
}



为什么我不应该只使用

Why I shouldn't just use

+=

编辑:好了,我现在知道如何重用的一个实例的StringBuilder (仍然不知道这是否是正确的代码标准),但是这是不值得只有一个<使用code>字符串,不是吗?

Ok, I know now how to reuse one instance of StringBuilder (still don't know if this is right with code standards), but this isn't worth to use with just one string, isn't it?

推荐答案

修改不可变的结构如字符串一定还是通过复制结构,并通过消耗更多的内存和减慢应用程序的运行时间(加上 GC 时,等等...)。

Modifying immutable structures like strings must be done by copying the structure and by that consuming more memory and slowing the application's run time (plus GC time, etc...).

的StringBuilder 来使用相同的可变对象的操作来解决这个问题。

StringBuilder comes to solve this problem by using the same mutable object for manipulations.

但是:

However:

字符串在编译时间如下:

string myString = "123";
myString += "234";
myString += "345";



它实际上将编译成这样的事情:

it will actually compile to something like that:

string myString = string.Concat("123", "234", "345");



这个功能比使用的StringBuilder 工作更快为字符串•输入功能是众所周知的。

this function is faster than working with StringBuilder for the number of strings entering the function is known.

所以编译时著名的<$ C的数量$ C>字符串串联,你应该更喜欢 string.Concat()

so for compile-time-known string concatenations you should prefer string.Concat().

作为像在下列情况下,数目不详字符串的:

as for unknown number of string like in the following case:

string myString = "123";
if (Console.ReadLine() == "a")
{
    myString += "234";
}
myString += "345";

现在的编译器不能使用 string.Concat()的功能,但是,的StringBuilder 似乎是在时间和内存消耗只有当连接用6-7或多个字符串

Now the compiler can't use the string.Concat() function, however, StringBuilder appears to be more efficient in time and memory consumption only when the concatenation is done with 6-7 or more strings.

坏习惯用法:

StringBuilder myString = new StringBuilder("123");
myString.Append("234");
myString.Append("345");



精细实践用法:

Fine practice usage:

StringBuilder myString = new StringBuilder("123");
if (Console.ReadLine() == "a")
{
    myString.Append("234");
}
myString.Append("345");



最佳实践用法:

Best practice usage:

StringBuilder myString = new StringBuilder("123");
while (Console.ReadLine() == "a")
{
    myString.Append("234"); //Average loop times 4~ or more
}
myString.Append("345");

这篇关于如何使用StringBuilder的明智的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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