Lua中怎么连接和格式化字符串

Lua
2695
2024/4/4 14:21:36
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

要连接和格式化字符串,在Lua中可以使用字符串连接运算符..和string.format()函数。

  1. 字符串连接:
local str1 = "Hello"
local str2 = "World"
local result = str1 .. " " .. str2
print(result)  -- 输出:Hello World
  1. 字符串格式化:
local name = "Alice"
local age = 25
local result = string.format("My name is %s and I am %d years old.", name, age)
print(result)  -- 输出:My name is Alice and I am 25 years old.

在string.format()中,%s表示字符串类型,%d表示整数类型,可以根据需要使用不同的格式化标识符。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: Lua中协程的使用场景有哪些