之前写过一个类似的,不过没严格按照UTF8编码规则判断。参考网上C代码,重新写一个符合UTF8编码规则的IsStringUTF8函数。
调用方法:
调用方法:
procedure TForm1.Button1Click(Sender: TObject); begin if IsStringUTF8(edit1.Text) then memo1.Lines.Add(edit1.Text+'--包含中文') else memo1.Lines.Add(edit1.Text+'--包含不中文'); end;
IsStringUTF8函数:
function IsStringUTF8(strtmp: string): Boolean; var nBytes: byte; chr: byte; bAllAscii: Boolean; i: Integer; begin nBytes := 0; bAllAscii := TRUE; for i := 1 to length(strtmp) do begin chr := ord(strtmp[i]); if (chr and $80) <> 0 then bAllAscii := FALSE; if nBytes = 0 then begin if chr >= $80 then begin if chr >= $FC then nBytes := 6 else if chr >= $F8 then nBytes := 5 else if chr >= $F0 then nBytes := 4 else if chr >= $E0 then nBytes := 3 else if chr >= $C0 then nBytes := 2 else Exit(FALSE); Dec(nBytes); end; end else begin if (chr and $C0) <> $80 then Exit(FALSE); Dec(nBytes); end; end; if nBytes > 0 then Exit(FALSE); if bAllAscii then Exit(FALSE); Result := TRUE; end;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2021-10-13 Lazarus Linux 创建菜单及桌面快捷方式