Length और Concatenation
# operator bytes में length देता है और .. strings को साथ जोड़ता है।
In this page:
Length और Concatenation
Length operator # bytes count करता है, characters नहीं, इसलिए non-ASCII text उम्मीद से ज़्यादा count हो सकता है। .. operator concatenate करता है और numbers को automatically strings में convert करता है। एक loop में कई .. से एक लंबी string बनाना slow है, इसलिए pieces को एक table में collect करें और table.concat इस्तेमाल करें।
Note:
UTF-8 text में characters count करने के लिए utf8.len(s) इस्तेमाल करें।
उदाहरण: Length and concatenation
local s = "Lua"
print(#s, #"", s .. " " .. 5.3)
print(1 .. 2)
print(#"héllo", utf8.len("héllo"))
local parts = {}
for i = 1, 5 do parts[#parts + 1] = i end
print(table.concat(parts, "-"))
-- Output:
-- 3 0 Lua 5.3
-- 12
-- 6 5
-- 1-2-3-4-5
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- बिना spaces के 1..2 लिखना, जो एक malformed number की तरह पढ़ा जाता है
- strings जोड़ने के लिए + इस्तेमाल करना
- यह मान लेना कि # UTF-8 text में characters count करता है
चैप्टर सारांश
- #s bytes में length है
- .. concatenate करता है
- Numbers .. से convert होते हैं
- कई pieces के लिए table.concat बेहतर है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: