请问您从UTF-8编码的文件中读取的字符是什么?
- 内容介绍
- 文章标签
- 相关推荐
本文共计304个文字,预计阅读时间需要2分钟。
是的,可以从文件中读取一个UTF-8字符。当使用`file.read(1)`返回奇怪的字符时,这可能是因为字符编码或读取方式的问题。你可以使用以下函数`firstLetter(str)`来从字符串`str`中返回一个UTF-8字符:
javascriptfunction firstLetter(str) { return str.match([\x01-\x7F][\x80-\xBF]*);}
这个函数使用正则表达式来匹配一个有效的UTF-8字符。你需要确保传递给这个函数的字符串是有效的UTF-8编码。
是否可以从文件中读取一个UTF-8字符?file:read(1)返回奇怪的字符,当我打印它时.
function firstLetter(str) return str:match("[%z\1-\127\194-\244][\128-\191]*") end
函数从字符串str返回一个UTF-8字符.我需要以这种方式读取一个UTF-8字符,但是从输入文件(不想将某些文件读取到内存中 – 通过文件:read(“* all”))
问题非常类似于这篇文章:
Extract the first letter of a UTF-8 string with Lua
function read_utf8_char(file) local c1 = file:read(1) local ctr, c = -1, math.max(c1:byte(), 128) repeat ctr = ctr + 1 c = (c - 128)*2 until c < 128 return c1..file:read(ctr) end
本文共计304个文字,预计阅读时间需要2分钟。
是的,可以从文件中读取一个UTF-8字符。当使用`file.read(1)`返回奇怪的字符时,这可能是因为字符编码或读取方式的问题。你可以使用以下函数`firstLetter(str)`来从字符串`str`中返回一个UTF-8字符:
javascriptfunction firstLetter(str) { return str.match([\x01-\x7F][\x80-\xBF]*);}
这个函数使用正则表达式来匹配一个有效的UTF-8字符。你需要确保传递给这个函数的字符串是有效的UTF-8编码。
是否可以从文件中读取一个UTF-8字符?file:read(1)返回奇怪的字符,当我打印它时.
function firstLetter(str) return str:match("[%z\1-\127\194-\244][\128-\191]*") end
函数从字符串str返回一个UTF-8字符.我需要以这种方式读取一个UTF-8字符,但是从输入文件(不想将某些文件读取到内存中 – 通过文件:read(“* all”))
问题非常类似于这篇文章:
Extract the first letter of a UTF-8 string with Lua
function read_utf8_char(file) local c1 = file:read(1) local ctr, c = -1, math.max(c1:byte(), 128) repeat ctr = ctr + 1 c = (c - 128)*2 until c < 128 return c1..file:read(ctr) end

