最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Lua判斷字符串中包含中文字符的方法和計算字符串寬度函數(shù)分享

 更新時間:2015年04月22日 11:53:01   投稿:junjie  
這篇文章主要介紹了Lua判斷字符串中包含中文字符的方法和計算字符串寬度函數(shù)分享,需要的朋友可以參考下

一、判斷字符串中包含中文字符的方法

遍歷數(shù)組,對每個字節(jié)使用string.byte(),發(fā)現(xiàn)有大于127的,就是漢字,可以參照下面的代碼。

二、計算字符串寬度函數(shù)

復(fù)制代碼 代碼如下:

-- 計算字符串寬度
 
local str = "Jimmy: 你好,世界!"
local fontSize = 20
local lenInByte = #str
local width = 0
 
for i=1,lenInByte do
    local curByte = string.byte(str, i)
    local byteCount = 1;
    if curByte>0 and curByte<=127 then
        byteCount = 1
    elseif curByte>=192 and curByte<223 then
        byteCount = 2
    elseif curByte>=224 and curByte<239 then
        byteCount = 3
    elseif curByte>=240 and curByte<=247 then
        byteCount = 4
    end
    
    local char = string.sub(str, i, i+byteCount-1)
    i = i + byteCount -1
    
    if byteCount == 1 then
        width = width + fontSize * 0.5
    else
        width = width + fontSize
        print(char)
    end
end
 
print("總寬度: "..width)

相關(guān)文章

最新評論

珠海市| 吴川市| 明水县| 怀宁县| 法库县| 东海县| 楚雄市| 南陵县| 琼海市| 宜州市| 织金县| 白银市| 栾城县| 抚远县| 黄大仙区| 桐庐县| 广元市| 南郑县| 广元市| 上虞市| 离岛区| 汕头市| 富锦市| 慈利县| 枣强县| 梧州市| 廊坊市| 布尔津县| 定西市| 平乡县| 通海县| 满城县| 崇阳县| 梁平县| 沙坪坝区| 曲沃县| 芜湖市| 宁城县| 平安县| 甘肃省| 日照市|