2016-08-18 62 views
2

我試圖將字符串與unicode字符對齊。
但它不起作用。
空格不正確。 :(
Lua的版本爲5.1。
這是什麼問題?
使用Lua使用unicode字符進行字符串格式化

local t = 
{ 
    "character", 
    "루아",   -- korean 
    "abc감사합니다123", -- korean 
    "ab23", 
    "lua is funny", 
    "ㅇㅅㅇ", 
    "美國大將",   --chinese 
    "qwert-54321", 
}; 

for k, v in pairs(t) do 
    print(string.format("%30s", v)); 
end 


result:---------------------------------------------- 
        character 
         루아 
      abc감사합니다123 
          ab23 
        lua is funny 
         ㅇㅅㅇ 
        美國大將 
        qwert-54321 
+0

格式化Unicode字符可能會非常棘手,因爲每個字符不僅在編碼時需要可變數量的字節,而且在顯示時還會使用可變數量的列。對Luarocks進行快速搜索時,[wcwidth](https://luarocks.org/modules/aperezdc/wcwidth)庫看起來與您正在嘗試執行的操作相關。 – hugomg

回答

2

的ASCII字符串的所有格式正確無誤,而對非ASCII字符串不是。

的原因是因爲,串的長度進行計數與它們的字節數。例如,對於UTF-8編碼,

print(string.len("美國大將")) -- 12 
print(string.len("루아"))  -- 6 

所以%sstring.format對待這兩個字符串,如果他們的寬度是12/6

+0

謝謝。瞭解。 :d – ddubie

0
function utf8format(fmt, ...) 
    local args, strings, pos = {...}, {}, 0 
    for spec in fmt:gmatch'%%.-([%a%%])' do 
     pos = pos + 1 
     local s = args[pos] 
     if spec == 's' and type(s) == 'string' and s ~= '' then 
     table.insert(strings, s) 
     args[pos] = '\1'..('\2'):rep(#s:gsub("[\128-\191]", "")-1) 
     end 
    end 
    return (fmt:format((table.unpack or unpack)(args)) 
     :gsub('\1\2*', function() return table.remove(strings, 1) end) 
    ) 
end 

local t = 
{ 
    "character", 
    "루아",   -- korean 
    "abc감사합니다123", -- korean 
    "ab23", 
    "lua is funny", 
    "ㅇㅅㅇ", 
    "美國大將",   --chinese 
    "qwert-54321", 
    "∞" 
}; 

for k, v in pairs(t) do 
    print(utf8format("%30s", v)); 
end 

但是還有一個問題,因爲:在大多數字體韓國和中國符號比拉丁字母寬。

相關問題