如何判断可见字符 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

原文地址:https://www.cnblogs.com/railgunman/p/9931247.html