ใช้คุณสมบัติ String.Length ใน C# เพื่อรับความยาวของสตริง
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