如何获得字符串中汉字和英文的个数

          Microsoft Word具有一项“字数统计”功能,可以统计出文本中汉字和英文的个数,其设计思路就是通过把字符转换为ASCII码数值来进行判断,Ord函数就可以把字符转换为对应的ASCII码数值,其中值为33-126为键盘可使用字符,值127以上的为未知字符,即汉字。下面完成一个示范程序,选择“File|New Application”菜单命令,在默认的窗体中添加1个Memo组件、2个Label组件和2个Button组件,完成对各个组件的属性设置及布局之后,在“字数统计”按钮的OnClick事件中添加如下所示的代码。

代码如下:
 
 1Procedure TForm1.Button1Click(Sender: TObject);
 2var s:string;
 3I,e,c:integer;
 4begin
 5s:=memo1.text; //Memo的内容
 6e:=0;c:=0;
 7for I:=1 to length(s) do
 8begin
 9if (ord(s[I])>=33)and(ord(s[I])<=126) then
10begin
11inc(e);
12label1.caption:='英文字数:'+inttostr(e);
13end
14else
15if (ord(s[I])>=127) then
16begin
17inc(c);
18label2.caption:='中文字数:'+inttostr(c div 2);
19end;
20end;
21end;
原文地址:https://www.cnblogs.com/sonicit/p/746614.html