📜  如何在C#中查找StringBuilder的长度

📅  最后修改于: 2021-05-29 22:54:37             🧑  作者: Mango

StringBuilder.Length属性用于获取或设置当前StringBuilder对象的长度。

下面的程序说明了上面讨论的属性的用法:

范例1:

// C# program to demonstrate
// the Length() Property
using System;
using System.Text;
  
class GFG {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String passed as parameter
        StringBuilder str = new StringBuilder("WelcomeGeeks");
  
        // print string
        Console.WriteLine("String = "
                   + str.ToString());
  
        // get length of StringBuilder object
        int length = str.Length;
  
        // print length
        Console.WriteLine("length of String = "
                                     + length);
    }
}
输出:
String = WelcomeGeeks
length of String = 12

范例2:

// C# program to demonstrate
// the Length() Property
using System;
using System.Text;
  
class GFG {
    public static void Main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String passed as parameter
        StringBuilder str = new StringBuilder("India is Great");
  
        // print string
        Console.WriteLine("String = "
                   + str.ToString());
  
        // get length of StringBuilder object
        int length = str.Length;
  
        // print length
        Console.WriteLine("length of String = "
                                     + length);
    }
}
输出:
String = India is Great
length of String = 14

参考:

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