您的位置:宽带测速网 > 编程知识 > Lua中怎么连接和格式化字符串

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

2025-06-25 15:38来源:互联网 [ ]

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

    字符串连接:
local str1 = "Hello"local str2 = "World"local result = str1 .. " " .. str2print(result)-- 输出:Hello World
    字符串格式化:
local name = "Alice"local age = 25local 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表示整数类型,可以根据需要使用不同的格式化标识符。

lua