local getutf8char = function(str,pos) localbyte = str:byte(pos); locallen,uchar = 1,""; ifbyte >= 0x00andbyte <= 0x7Fthen len = 1 elseifbyte >= 0xC2andbyte <= 0xDFthen len = 2 elseifbyte >= 0xE0andbyte <= 0xEFthen len = 3 elseifbyte >= 0xF0andbyte <= 0xF4then len = 4 else returnlen,uchar; end uchar = str:sub(pos,pos + len - 1); returnlen,uchar; end
local s = "你好hello繁体字123こんにちは안녕하세요<>~!@#《》" local pos = 1; while pos < #s do locallen,char = getutf8char(s,pos); print("pos",pos,"len",len,"char",char); pos = pos + len; end
local uchars_1 = function(str) local pos = 1 local nextchar = function() if pos > #str thenreturnnilend locallen,char = getutf8char(str,pos) pos=pos + len returnchar end return nextchar,str,pos end