美文网首页
Lua 的utf8库及使用方法

Lua 的utf8库及使用方法

作者: cherishpf | 来源:发表于2019-08-19 15:11 被阅读0次

a utf-8 support module for Lua and LuaJIT
源码地址:https://github.com/starwing/luautf8

编译后可用的库:https://github.com/cherishpf/luautf8
https://download.csdn.net/download/cherishpf/11579451
Linux版:lua-utf8.so
Windows版:lua-utf8.dll(若是用在openresty中,openresty版本需使用32位版本,使用64位版本时会报错“lua-utf8.dll 不是有效的 Win32 应用程序”)
将lua-utf8库放在openresty安装目录下,使用时用require引入。

local utf8 = require('lua-utf8')

local s1 = "我是utf-8编码的字符串ABC"

-- s1的长度:16
print("s1的长度:" .. utf8.len(s1))

-- 在第3个字处插入:我是hello哈utf-8编码的字符串ABC
print(utf8.insert(s1, 3, "hello哈"))
-- 在字符串末尾插入:我是utf-8编码的字符串ABChello哈
print(utf8.insert(s1, 0, "hello哈"))
print(utf8.insert(s1, "hello哈"))
-- 在字符串末尾前1个字处插入:我是utf-8编码的字符串ABhello哈C
print(utf8.insert(s1, -1, "hello哈"))
-- 在字符串开头插入:hello哈我是utf-8编码的字符串ABC
print(utf8.insert(s1, -utf8.len(s1), "hello哈"))

-- 大写字母变成小写:我是utf-8编码的字符串abc
print(utf8.lower(s1))
print(utf8.fold(s1))

-- 小写字母变成大写:我是UTF-8编码的字符串ABC
print(utf8.upper(s1))
print(utf8.title(s1))

-- 反转字符串:CBA串符字的码编8-ftu是我
print(utf8.reverse(s1))

-- 截取子串:我是utf-8编码
print(utf8.sub(s1, 1, 9))

-- 返回子串的码值:25105    26159   117
print(utf8.byte(s1, 1, 3))
-- 返回码值对应的字符:我
print(utf8.char(25105))

-- 查找第一次匹配到的字符串(默认从第1个字符起开始查找):字符串
print(utf8.match(s1, "字符串"))
-- 返回第一次匹配到的字符串的开始和结束位置(默认从第1个字符起开始查找):11   13
print(utf8.find(s1, "字符串"))

--打印第一个字的字符位置(以字节为单位)及Unicode码值:1   25105
print(utf8.next(s1))
for pos, code in utf8.next, s1 do
  -- 循环打印字符串中每个字的编码位置及编码值
  print("处于第"..pos.."位的字符Unicode码为: "..code)
end

-- Unicode码转义为utf-8编码
local u = utf8.escape
-- 中文%123?d%u榄
print(u"%20013%25991%%123%?%d%%u%27012")

print(utf8.escape("%123%u123%{123}%u{123}%xABD%x{ABD}"))

相关文章

网友评论

      本文标题:Lua 的utf8库及使用方法

      本文链接:https://www.haomeiwen.com/subject/gagssctx.html