您的位置:首页 > 其它

判断一个字符是否为汉字的最佳方法[转]

2008-11-14 16:26 453 查看
由于从 Delphi2005开始支持中文标识符,在编写 PASCAL 词法分析器的过程中遇到了这个问题,经过多次试验找到了解决方案,至今未发现问题。

代码如下:

//判断字符是否是汉字 function IsHZ(ch: WideChar): boolean; var i:integer; begin i:=ord(ch); if( i<19968) or (i>40869) then result:=false else result:=true; end;
//判断字符是否是汉字
function IsHZ(ch: WideChar): boolean;
var
i:integer;
begin
i:=ord(ch);
if( i<19968) or (i>40869) then
result:=false else result:=true;
end;

2005年1月28日:感谢滚龙的指点,已将代码改写如下:

//判断字符是否是汉字 function TForm1.IsHZ(ch: Char): boolean; begin //返回值为 0 的时候为单字节字符,返回值为 1 的时候为多字节字符 if(ord(bytetype(ch,1))=1) then result:=true else result:=false; end;
//判断字符是否是汉字
function TForm1.IsHZ(ch: Char): boolean;
begin
//返回值为 0 的时候为单字节字符,返回值为 1 的时候为多字节字符
if(ord(bytetype(ch,1))=1) then result:=true
else result:=false;
end;

2005年1月31日:谢谢滚龙再次赐教,代码已收藏!

//判断字符是否是汉字
function IsMBCSChar(const ch: Char): Boolean;
begin
Result := (ByteType(ch, 1) <> mbSingleByte);
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: