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

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

问题描述

首先,我对使用StringBuilder类感到困惑:

I am little confused about using StringBuilder class, first:

string对象的串联操作始终根据现有的string和新数据创建一个新对象. 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实例以避免创建String的新实例在哪里呢?听起来像一对一"交易.

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();
}

为什么我不应该只用

+=

好的,我现在知道如何重用StringBuilder的一个实例(仍然不知道这是否符合代码标准),但这不值得只使用一个string,不是吗?

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?

推荐答案

修改 不可变 结构,例如 string 必须通过复制结构来完成,从而消耗更多的内存并减慢应用程序的运行时间(还会增加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 (also increasing GC time, etc...).

StringBuilder通过使用相同的可变对象进行操作来解决此问题.

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

但是:

However:

在编译时按以下方式连接string时:

when concatenating a string in compile time as the following:

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

它实际上会编译成类似这样的东西:

it will actually compile to something like that:

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

此功能比使用StringBuilder更快,因为已知string进入该功能的次数.

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

因此,对于编译时已知的string串联,您应该首选string.Concat().

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

对于string的未知数,如以下情况:

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

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

现在,编译器无法使用string.Concat()函数,但是,仅当使用6-7或更多的strings进行串联时,StringBuilder似乎在时间和内存消耗上更有效.

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");

优良作法的用法(请注意使用了if):

Fine practice usage (note that if is used):

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

最佳实践用法(请注意使用了while循环):

Best practice usage (note that while loop is used):

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天全站免登陆