本文整理自网络,侵删。
想在文本控件TMemo中,分别对中、英文的字符数进行统计,我们可以通过把字符转换为数值来进行判断,Ord()函数把字符转换为对应的数值,值33-126为键盘可使用字符,值127以上的为未知字符,即为汉字.
代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
i,e,c:integer;
begin
s:=memo1.text;
e:=0;c:=0;
for i:=1 to length(s) do
begin
if (ord(s[i])>=33)and(ord(s[i])<=126) then
begin
inc(e);
label1.caption:='英文个数:'+inttostr(e);
end
else
if (ord(s[i])>=127) then
begin
inc(c);
label2.caption:='中文个数:'+inttostr(c div 2);
end;
end;
end;
相关阅读 >>
Delphi 请求时间,为当前时间,数值为1970-01-01以来的毫秒数
Delphi xe5 将Delphi code从winos 迁移到ios与android
Delphi xe6 android 界面皮肤美化 用stylebook
Delphi中exit,abort,break,continue,halt,runerro 的区别
更多相关阅读请进入《Delphi》频道 >>