字符串插值

基本类型 / 字符串

local font_size = 14
local font_family = "Arial"

-- format: %s takes any value, as text
local style1 = string.format(
  'font-size: %s;font-family: "%s"',
  font_size, font_family)
-- style1 = font-size: 14;font-family: "Arial"

-- .. joins text and numbers, any expression
local style2 = "font-size: " .. font_size / 2
               .. ";font-family: " .. font_family
-- style2 = font-size: 7.0;font-family: Arial

-- A template: gsub looks each name up in a table
local template = "font-size: ${size}"
local style3 = template:gsub("%${(%w+)}",
                             {size = font_size})
-- style3 = font-size: 14

print("style1 = " .. style1)
print("style2 = " .. style2)
print("style3 = " .. style3)