ToLua中utf8库的使用
local utf8 = utf8

function Test()        
    local l1 = utf8.len('你好') --计算长度
    print('chinese string len is: '..l1)     

    local s = '遍历s字符串'                                        

    --i为每个字符的开始位置 next为下一个字符的开始位置
    for i in utf8.byte_indices(s) do            
        local next = utf8.next(s, i) 
        print(s:sub(i, next and next - 1)) --截取出来就是当前的字符
    end   

    local s1 = '天下风云出我辈风云'        
    print('风云 count is: '..utf8.count(s1, '风云')) --计算风云在字符串中的个数
    s1 = s1:gsub('风云', '風雲') --替换并返回整个串
    print(s1)
end

首页 我的博客
粤ICP备17103704号