delphi判断一个字符是否为汉字的最佳方法  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi判断一个字符是否为汉字的最佳方法


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

代码如下:
view plaincopy to clipboardprint?
//判断字符是否是汉字
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日:感谢滚龙的指点,已将代码改写如下:
view plaincopy to clipboardprint?
//判断字符是否是汉字
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日:谢谢滚龙再次赐教,代码已收藏!
view plaincopy to clipboardprint?
//判断字符是否是汉字
function IsMBCSChar(const ch: Char): Boolean;
begin
Result := (ByteType(ch, 1) <> mbSingleByte);
end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.042860984802246 seconds