UTF-8字符串在lua中的字长问题
来源:互联网 发布:bi开发工程师转行java 编辑:程序博客网 时间:2024/06/05 23:53
UTF-8字符串在lua中每个字符的字长是不一样,中文,英文,其他字符。都有各自不同的字长,所以UTF-8也叫作变长字符。规则如下:
1. utf8字符是变长字符
2. 字符长度有规律
UTF-8字符规律
字符串的首个byte表示了该utf8字符的长度
0xxxxxxx - 1 byte
110yxxxx - 192, 2 byte
1110yyyy - 225, 3 byte
11110zzz - 240, 4 byte
local UTF8 = {}function UTF8.chSize(char) if not char then return 0 elseif char > 240 then return 4 elseif char > 225 then return 3 elseif char > 192 then return 2 else return 1 endendfunction UTF8.sub(str, startChar, numChars) if str == nil then return "" end local startIndex = 1 if (startChar==nil) then startChar = 1; end if (numChars==nil) then numChars =15; end; local allChars = numChars while startChar > 1 do local char = string.byte(str, startIndex) startIndex = startIndex + UTF8.chSize(char) startChar = startChar - 1 end local currentIndex = startIndex while currentIndex <= numChars and currentIndex <= #str do local char = string.byte(str, currentIndex) currentIndex = currentIndex + UTF8.chSize(char) end if numChars < #str then return str:sub(startIndex, currentIndex - 1).."..." else return str:sub(startIndex, currentIndex - 1) end endfunction UTF8.length(str)local length = 0;local currentIndex = 1;while currentIndex <= #str do local char = string.byte(str, currentIndex) currentIndex = currentIndex + UTF8.chSize(char) length = length + 1 end return length;endfunction UTF8.toString(str) if type(str)=="string" then return str end local char = "" if type(str)~="table" then return char end for key, var in pairs(str) do if var == "\0" then break end char = char .. var end return charendreturn UTF8;
阅读全文
0 0
- UTF-8字符串在lua中的字长问题
- Lua中对于UTF-8字符串的处理
- lua utf-8
- Lua 删除UTf-8编码中的特殊字符
- UTF-8编码字符串的Trim() 在.35和4.0中的行为区别
- TinyXML 解析 UTF-8 字符串的问题
- 将URL中的UTF-8字符串转成中文字符串
- LUA中的字符串函数库
- Lua中的字符串函数库
- Lua中的字符串函数库
- Lua中的字符串函数库
- Lua中的字符串函数库
- Lua中的字符串函数库
- Lua中的字符串函数库
- Lua中的字符串函数库
- Lua中的字符串函数库
- Lua中的字符串函数库
- Lua中的字符串函数库
- phpStorm设置显示代码行号
- Matlab的regionprops详解
- 队列实现栈,栈实现队列
- UVA 11029 Leading and Trailing
- JavaScript资源大全
- UTF-8字符串在lua中的字长问题
- 消息列队
- mysql--数据操纵语言DML
- jQuery学习笔记
- 【数据库系统概念】第4章 中级SQL 知识总结
- 使用类的重载实现不同数据类型之间的运算
- 不使用第三个变量逆置字符串
- 你知道position吗?
- dsoframer-在线编辑office文档,一款开源的由微软提供