Bash:
स्ट्रिंग की लंबाई ज्ञात करना
How to: (कैसे करें:)
Bash me string ki length pata karna simple hai. Neeche di gai code me dekho:
# String ko variable me save karo
my_string="Hello, नमस्ते!"
# {#variable} use karte hue length pata karo
length=${#my_string}
echo "String length is: $length"
Output:
String length is: 16
Deep Dive (गहन जानकारी)
Bash me string ki length pata karne ka tarika kafi seedha hai, jo ki shell scripting ke shuruaati version se hi uplabdh hai. #
symbol ka prayog variable ke naam ke saath kiya jaata hai, jo ki string ke characters ki sankhya ko lautaata hai.
Kuch aur tariko se bhi hum length pata kar sakte hain, jaise ki expr
command ka use karke:
length_with_expr=$(expr length "$my_string")
echo "String length with expr is: $length_with_expr"
Ya fir awk
ka use karke:
length_with_awk=$(echo "$my_string" | awk '{print length}')
echo "String length with awk is: $length_with_awk"
Lekin aam taur par, ${#variable}
syntax sabse saaf aur tez hota hai.