📜  C#中的StringBuilder.ToString方法

📅  最后修改于: 2021-05-29 16:39:56             🧑  作者: Mango

此方法用于将该实例的值转换为String。创建并初始化一个新的String对象,以从此StringBuilder对象获取字符序列,然后由ToString()返回String。 Object包含的对该序列的后续更改不会影响String的内容。

下面的程序说明了StringBuilder.ToString()方法:

范例1:

// C# program to demonstrate
// the ToString() Method
using System;
using System.Text;
  
class GFG {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder str
          = new StringBuilder("GeeksForGeeks");
  
        // print string
        Console.WriteLine("String contains = "
                            + str.ToString());
    }
}
输出:
String contains = GeeksForGeeks

范例2:

// C# program to demonstrate
// the ToString() Method
using System;
using System.Text;
  
class GFG {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder str = 
             new StringBuilder("GeeksforGeeks Contribute");
  
              
                  
        // print string
        Console.WriteLine("String contains = "
                          + str.ToString());
    }
}
输出:
String contains = GeeksforGeeks Contribute

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.text.stringbuilder.tostring?view=netframework-4.7.2