Lua 字符间距完美

失败的间距

1

我正在尝试通过为每个玩家名称添加所需的空格数,使所有具有最大 31 个字符的名称在同一行上对齐。我已经尝试了一段时间,但我完全搞不清楚。

这是我的当前代码,虽然很糟糕,但我知道……

local c = client.GetPlayerNameByIndex(i)
if c ~= nil and client.GetPlayerNameByIndex(i) ~= "nil" then
    playerlist[i] = all_trim(client.GetPlayerNameByIndex(i))

    local leve = 67
    local namelength = #client.GetPlayerNameByIndex(i) --最大 31 个字符
    local output = "" .. client.GetPlayerNameByIndex(i)
    local newspace = ""
    local neededspaces = 31 - #client.GetPlayerNameByIndex(i)
    print(neededspaces)
    for i=1, 31-#string.sub(client.GetPlayerNameByIndex(i), 1, 31) do
        newspace = newspace .. " "
    end
    playerinfolist[i] = output .. newspace .. "a"

end

简单来说,我希望所有的 "a" 都与每个字符串对齐。感谢你的帮助!

点赞