您的位置:首页 > 其它

判断一个字符是否为汉字的最佳方法 字数,字符,字节统计

2009-06-30 15:57 726 查看
procedure Tfrm1.memoTextChange(Sender: TObject);

var
  i,e,c,c2,w:integer;
  s:String;

//s:wideString;

// 注:wideString类型,也可以统计
begin
  s:=memoText.Text;
  e:=0;
  c:=0;
  if s='' then
  begin
    lblWordHint.Caption:='字数:0';
    abort;
  end;

  for I := 0 to length(s) do
  begin
    if (ord(s[i])>=32) and (ord(s[i])<=126) then
    begin
      inc(e);
    end
    else
    if (ord(s[i])>126) then
    begin
      inc(c);
    end;
  end;
    c2:=c div 2;
    w:=e+c2;
   if length(s)>255 then
  begin
    lblWordHint.Caption:='字节过多!字数:汉字:'+intToStr(c2)+',英文字符:'+intToStr(e)+',共:'+intToStr(w)+'字'+intToStr(length(s))+'字节';
    lblWordHint.Color:=clRed;
    abort;
  end
  ELSE
  BEGIN
    lblWordHint.Caption:='字数:汉字:'+intToStr(c2)+',英文字符:'+intToStr(e)+',共:'+intToStr(w)+'字'+intToStr(length(s))+'字节';
    lblWordHint.Color:=clBtnFace;
  END;
 
end;

 

 

附注:

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

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