调用IsStringUTF8来判断string是否包含UTF8(中文);
procedure TForm1.Button1Click(Sender: TObject); begin if IsStringUTF8(edit1.Text) then memo1.Lines.Add(s+'--包含中文') else memo1.Lines.Add(s+'--包含不中文'); end;
function IsStringUTF8(strtmp: string): Boolean; var nBytes: byte; chr: byte; bAllAscii: Boolean; i: Integer; lengths:integer; str:PChar; begin lengths:=length(strtmp); nBytes := 0; bAllAscii := TRUE; for i := 1 to lengths 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;
标签:begin,end,UTF8,else,chr,lazarus,nBytes,字符串,FALSE From: https://www.cnblogs.com/qiufeng2014/p/18462523