如何使用C#计算字符串的长度?

csharpprogrammingserver side programming更新于 2025/5/30 0:22:17

使用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

相关文章