1. 程式人生 > >使用lua實現擷取中英文字串

使用lua實現擷取中英文字串

--返回當前字元實際佔用的字元數
local function SubStringGetByteCount(str, index)
    local curByte = string.byte(str, index)
    local byteCount = 1;
    if curByte == nil then
        byteCount = 0
    elseif 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 return byteCount; end --獲取中英混合UTF8字串的真實字元數量 local function SubStringGetTotalIndex(str) local curIndex = 0
; local i = 1; local lastCount = 1; repeat lastCount = SubStringGetByteCount(str, i) i = i + lastCount; curIndex = curIndex + 1; until(lastCount == 0); return curIndex - 1; end --獲取字串的真實索引值 local function SubStringGetTrueIndex(str, index) local curIndex = 0
; local i = 1; local lastCount = 1; repeat lastCount = SubStringGetByteCount(str, i) i = i + lastCount; curIndex = curIndex + 1; until(curIndex >= index); return i - lastCount; end --擷取中英混合的UTF8字串,endIndex可預設 function StringUtil.SubStringUTF8(str, startIndex, endIndex) if startIndex < 0 then startIndex = SubStringGetTotalIndex(str) + startIndex + 1; end if endIndex ~= nil and endIndex < 0 then endIndex = SubStringGetTotalIndex(str) + endIndex + 1; end if endIndex == nil then return string.sub(str, SubStringGetTrueIndex(str, startIndex)); else return string.sub(str, SubStringGetTrueIndex(str, startIndex), SubStringGetTrueIndex(str, endIndex + 1) - 1); end end

呼叫:
local str = “abc中文”
print(StringUtil.SubStringUTF8(str,4,5))
print(StringUtil.SubStringUTF8(str,1,4))
輸出結果:
中文
abc中
[Finished in 0.1s]