如何使用C#计算字符串的长度?
在C#中使用String.Length属性获取字符串的长度。
str.Length
该属性计算字符串中的单词并显示指定字符串的长度,例如,字符串Amit有4个字符-
string str = "Amit";
示例
以下是用于计算字符串长度的C#程序-
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
string str = "Amit";
Console.WriteLine("String: "+str);
Console.WriteLine("String Length: "+str.Length);
Console.ReadKey();
}
}
}输出结果
String: Amit String Length: 4