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;