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