StringBuilder.Length属性用于获取或设置当前StringBuilder对象的长度。
【如何在C#中查找StringBuilder的长度()】语法:public int Length {get; set; }返回当前实例的长度。下面的程序说明了上面讨论的属性的用法:
异常:如果为set操作指定的值小于零或大于MaxCapacity, 则此属性将提供ArgumentOutOfRangeException。
示例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/en-us/dotnet/api/system.text.stringbuilder.length?view=netframework-4.7.2
推荐阅读
- 如何在PHP中生成随机、唯一的字母数字字符串
- 如何在Golang中找到指针的长度()
- 如何在C#中查找数组的长度()
- 什么是SEO文章(你为什么要写呢?以及如何写?)
- SEO关键词(如何选择更好的关键词进行优化())
- 如何在Python中获取类属性列表()
- 如何在Linux中安装Scala(详细步骤图解)
- 如何为C#安装和设置Visual Studio(步骤图解)
- 如何安装Android虚拟设备(AVD)(详细图解步骤)