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