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

更新时间: 2020 年 6 月 20 日

已阅读 1.1 万次

开启您的 职业生涯

完成课程即可获得认证

立刻开始
广告
© . All rights reserved.