Dùng thuộc tính String.Length trong C# để lấy độ dài chuỗi.
str.Length
Thuộc tính đếm ký tự trong chuỗi và hiển thị độ dài, ví dụ chuỗi "Amit" có 4 ký tự:
string str = "Amit";
Ví dụ
Chương trình C# tính độ dài chuỗi:
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();
}
}
}
Kết quả
String: Amit
String Length: 4