如何在 C# 中创建 StringBuilder?


在 C# 中创建 StringBuilder 的代码如下 −

实例

 实时演示

using System;
using System.Text;
public class Demo {
   public static void Main(String[] args) {
      StringBuilder strBuilder1 = new StringBuilder("Tim");
      StringBuilder strBuilder2 = new StringBuilder("Tom");
      Console.WriteLine("Is StringBuilder1 equal to StringBuilder2? = "+strBuilder1.Equals(strBuilder2));
   }
}

输出

以下是输出内容 −

Is StringBuilder1 equal to StringBuilder2? = False

实例

让我们看另一个示例 −

 实时演示

using System;
using System.Text;
public class Demo {
   public static void Main(String[] args) {
      StringBuilder strBuilder1 = new StringBuilder("John");
      StringBuilder strBuilder2 = new StringBuilder("John");
      StringBuilder strBuilder3 = new StringBuilder();
      StringBuilder strBuilder4 = new StringBuilder(5);
      strBuilder2 = strBuilder3;
      Console.WriteLine("Is StringBuilder3 equal to StringBuilder2? = "+strBuilder3.Equals(strBuilder2));
      Console.WriteLine("StringBuider1 capacity = "+strBuilder1.Capacity);
      Console.WriteLine("StringBuider2 capacity = "+strBuilder2.Capacity);
      Console.WriteLine("StringBuider3 capacity = "+strBuilder3.Capacity);
      Console.WriteLine("StringBuider4 capacity = "+strBuilder4.Capacity);
   }
}

输出

以下是输出内容 −

Is StringBuilder3 equal to StringBuilder2? = True
StringBuider1 capacity = 16
StringBuider2 capacity = 16
StringBuider3 capacity = 16
StringBuider4 capacity = 5

更新于: 10-12-2019

102 浏览量

开启职业生涯

通过完成课程获得认证

立即开始
广告