local font_size = 14
local font_family = "Arial"
-- format 中的 %s 可以接受任何值,并转为文本
local style1 = string.format(
'font-size: %s;font-family: "%s"',
font_size, font_family)
-- style1 = font-size: 14;font-family: "Arial"
-- .. 连接文本和数字,可以是任意表达式
local style2 = "font-size: " .. font_size / 2
.. ";font-family: " .. font_family
-- style2 = font-size: 7.0;font-family: Arial
-- 模板:gsub 在表中查找每个名称
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)