← Back to Lua Course | Chapter 3: Strings | Lesson 2 of 7

Length और Concatenation

# operator bytes में length देता है और .. strings को साथ जोड़ता है।

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

lua
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
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना spaces के 1..2 लिखना, जो एक malformed number की तरह पढ़ा जाता है
  2. strings जोड़ने के लिए + इस्तेमाल करना
  3. यह मान लेना कि # UTF-8 text में characters count करता है
चैप्टर सारांश
  • #s bytes में length है
  • .. concatenate करता है
  • Numbers .. से convert होते हैं
  • कई pieces के लिए table.concat बेहतर है

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.