中英文字符统计 Delphi代码
- 标签:字符统计 更新时间:2014-07-31
本Delphi代码实现一段字符的分类统计,可分别统计出字符串中的英文字符数量和中文汉字数量,中文和英文在计算机中是区别对待的,因此本代码抓住这一特点,实现字符的分类统计,具体代码:
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Label1: TLabel; Label2: TLabel; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} 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; end.
- 已读:
次 收藏本文关闭本文打印本文复制链接