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

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

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

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

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

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

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

-- 計(jì)算字符串寬度
 
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)文章

最新評論

措美县| 桂阳县| 浦东新区| 金塔县| 香港| 阳江市| 瑞安市| 陇南市| 武胜县| 赤峰市| 东方市| 南郑县| 卢湾区| 嫩江县| 济阳县| 西林县| 高陵县| 彰化市| 班戈县| 香格里拉县| 木里| 德格县| 天柱县| 孝感市| 会同县| 哈尔滨市| 横山县| 临海市| 衡阳市| 东丽区| 江永县| 册亨县| 白山市| 炉霍县| 方正县| 五峰| 平原县| 衡阳市| 犍为县| 桐乡市| 鲁山县|