1. 程式人生 > >如何判斷可見字元 Unicode

如何判斷可見字元 Unicode

一個Unicode字串,如何判斷其中都是可見字元?

//根據國標 GB2312 的中文漢字及符號 區位碼的範圍判斷
Function CheckIsGB2312(Char : WideChar) : Boolean;
var
  S : AnsiString;
begin
  S := Char;
  Result := (PByte(integer(S)+1)^>=$A1) and (PByte(integer(S)+1)^<=$FE) and
            (PByte(S)^>=$B0) and (PByte(S)^<=$F7);
end
; //檢查是否都是可見英文字元或者漢字及符號,全部是返回True否則False,空格認為可見 Function StrIsCanShow(Const WS : WideString) : Boolean; var i : integer; P : PWideChar; begin Result := True; P := Pointer(WS); for i:=1 to Length(WS) do begin if not ( ((PWord(P)^>=$20) and (PWord(P)^<=$7E)) //
Ansi 可見字元 or CheckIsGB2312(P^) //GB2312漢字及符號 ) then begin Result := False; Break; end; Inc(P); end; end;

注意string從來不是widestring,D2009之前string是ansistring, 從D2009開始,string是unicodestring。
unicodestring和widestring雖然都是UTF-16字串,但不是同一種類型,unicodestring是Delphi原生型別,支援引用計數和內碼表,使用Delphi RTL記憶體管理,

widestring是Windows COM BSTR型別的封裝,不支援引用計數和內碼表,使用Windows記憶體管理。

 

https://bbs.csdn.net/topics/370029342

https://bbs.csdn.net/topics/392047346?page=1