c#

c#字符串长度如何计算

小亿
112
2024-05-20 09:16:27
栏目: 编程语言

在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("字符串包含字符");

}

```

0
看了该问题的人还看了