在C#中,可以使用字符串的Length属性来计算字符串的长度。示例如下:
```csharp
string str = "Hello, World!";
int length = str.Length;
Console.WriteLine("字符串的长度为:" + length);
```
上述代码会输出:字符串的长度为:13
另外,还可以使用字符串的Length属性来判断字符串是否为空或者是否包含任何字符,示例如下:
```csharp
string emptyStr = "";
if (emptyStr.Length == 0)
{
Console.WriteLine("字符串为空");
}
string str = "Hello, World!";
if (str.Length > 0)
{
Console.WriteLine("字符串包含字符");
}
```
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:c语言字符串长度如何计算