【问题标题】:StringBuilder: how to get the final String?StringBuilder:如何获得最终的字符串?
【发布时间】:2008-10-22 22:21:25
【问题描述】:

有人告诉我,用 StringBuilder 连接字符串更快。我已经更改了我的代码,但我没有看到任何属性或方法来获取最终的构建字符串。

如何获取字符串?

【问题讨论】:

    标签: c# string stringbuilder


    【解决方案1】:

    您可以使用.ToString()StringBuilder 获取String

    【讨论】:

    • 是 toString 而不是 ToString
    • @lok​​i 对于 C#,它是 ToString
    【解决方案2】:

    使用 StringBuilder 完成处理后,使用 ToString 方法返回最终结果。

    来自 MSDN:

    using System;
    using System.Text;
    
    public sealed class App 
    {
        static void Main() 
        {
            // Create a StringBuilder that expects to hold 50 characters.
            // Initialize the StringBuilder with "ABC".
            StringBuilder sb = new StringBuilder("ABC", 50);
    
            // Append three characters (D, E, and F) to the end of the StringBuilder.
            sb.Append(new char[] { 'D', 'E', 'F' });
    
            // Append a format string to the end of the StringBuilder.
            sb.AppendFormat("GHI{0}{1}", 'J', 'k');
    
            // Display the number of characters in the StringBuilder and its string.
            Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());
    
            // Insert a string at the beginning of the StringBuilder.
            sb.Insert(0, "Alphabet: ");
    
            // Replace all lowercase k's with uppercase K's.
            sb.Replace('k', 'K');
    
            // Display the number of characters in the StringBuilder and its string.
            Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());
        }
    }
    
    // This code produces the following output.
    //
    // 11 chars: ABCDEFGHIJk
    // 21 chars: Alphabet: ABCDEFGHIJK
    

    【讨论】:

      【解决方案3】:

      关于它更快/更好的内存:

      我用 Java 研究过这个问题,我认为 .NET 会很聪明。

      String 的实现令人印象深刻。

      String 对象跟踪“长度”和“共享”(与保存字符串的数组的长度无关)

      类似

      String a = "abc" + "def" + "ghi";
      

      可以(由编译器/运行时)实现为:

      - 将包含“abc”的数组扩展 6 个额外的空格。 - 在 abc 之后复制 def - 在 def 之后复制 ghi。 - 将指向“abc”字符串的指针指向 a - 将 abc 的长度设置为 3,将 a 的长度设置为 9 - 在两者中设置共享标志。

      由于大多数字符串都是短暂的,因此在许多情况下这会产生一些非常有效的代码。 绝对没有效率的情况是当你在循环中添加一个字符串,或者你的代码是这样的:

      a = "abc";
      a = a + "def";
      a += "ghi";
      

      在这种情况下,最好使用 StringBuilder 构造。

      我的观点是,每次优化时都应该小心,除非您绝对确定自己知道自己在做什么,并且绝对确定这是必要的,并且您进行测试以确保优化的代码能够通过用例,只需以最易读的方式对其进行编码,不要试图超越编译器。

      在查看字符串源代码并发现编译器已经比我的用例做得更好之前,我浪费了 3 天时间来处理字符串、缓存/重用字符串构建器和测试速度。然后我不得不解释为什么我真的不知道我在做什么,我只是以为我知道了......

      【讨论】:

        【解决方案4】:

        当您说“将 String 与 String builder 连接起来更快”时,仅当您 反复(我重复 - 反复)连接到同一个对象。

        如果您只是连接 2 个字符串并立即将结果作为 string 进行处理,那么使用 StringBuilder 是没有意义的。

        我只是偶然发现了 Jon Skeet 的精彩文章:http://www.yoda.arachsys.com/csharp/stringbuilder.html

        如果您使用的是StringBuilder,那么要获得结果string,只需调用ToString()(不出所料)。

        【讨论】:

          【解决方案5】:

          我只是想扔掉它可能不一定更快,它肯定会有更好的内存占用。这是因为字符串在 .NET 中是不可变的,每次更改字符串时都会创建一个新字符串。

          【讨论】:

            【解决方案6】:

            concat 并不快 - 正如 smaclell 指出的那样,问题是不可变字符串强制额外分配和重新复制现有数据。

            "a"+"b"+"c" 与字符串生成器的关系并不快,但是随着 concat 的 # 变大,使用中间字符串的重复 concat 会变得越来越快,例如:

            x = "a"; x+="b"; x+="c"; ...

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-06-09
              • 2010-09-09
              • 1970-01-01
              • 2015-05-13
              • 2020-06-18
              • 1970-01-01
              相关资源
              最近更新 更多