lua中怎么做到类似C++用下标获得那个位置的字符。utf-8的。

lua中怎么做到类似C++用下标获得那个位置的字符。utf-8的。

字符串中有英文和汉字。

自顶!

–根据长度截取 包括中文(minLen截取起始位置)

function gFunUtils.subToLen(str,strLen,minLen)
    local lenInByte = #str
    local len = 0
    local index = 1
    local minInByte = 2
    
    while(lenInByte > 0) do
        local curByte = string.byte(str, index)
        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
        elseif curByte>=248 and curByte<=251 then
            byteCount = 5
        elseif curByte>=252 and curByte<=253 then
            byteCount = 6
        end                                              
        if byteCount > 1 then
            lenInByte = lenInByte - byteCount
            index = index + byteCount
            len = len + 1
        else
            lenInByte = lenInByte - byteCount
            index = index + byteCount
            len = len + 1
        end

        if minLen and len == minLen then
            minInByte = index - byteCount
        end
        if strLen == len then
            if minLen then
                return string.sub(str,minInByte,index-1)
            else
                --todo
                return string.sub(str,1,index-1)
            end
        end
    end

    return str
end

无法用下标获取字符串的char。。。
我一般都是string.sub(str, 5, 5)