如何使用 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP