TypeScript:
स्ट्रिंग की लंबाई ज्ञात करना
How to: (कैसे करें:)
TypeScript me string ki length nikalne ke liye .length
property ka istemaal hota hai. Simple hai, dekho:
let greeting: string = "नमस्ते";
console.log(greeting.length); // Output: 6
Ye 6
output dikha raha hai, kyunki “नमस्ते” me 6 characters hain.
Deep Dive (गहराई में जानकारी)
Historically, JavaScript (aur TypeScript, jo ki JavaScript par based hai) me string ki length property hamesha se rahi hai. Par dhyan rakho, Unicode characters ko count karte waqt, jaise ki emojis ya kuch special symbols, to .length
thoda differently behave kar sakta hai.
Alternatives ke roop me, libraries jaise ki lodash
ya underscore
ko use kar sakte hain, agar aapko advanced string manipulation ki zarurat ho.
Implementation detail: TypeScript ki .length
property string ki length ko ek number ke roop me return karti hai. Ye UTF-16 code units ko count karta hai. Iska matlab hai ki agar aapko rare Unicode characters milte hain (jo ki surrogate pairs ka use karte hain), to .length
accurate na ho.