如何在 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

更新日期: 20-06-2020

11K+ 浏览量

事业起步

完成课程后获得认证

开始
广告
© . All rights reserved.