统计中英文个数:想在文本控件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;
统计中英文个数:想在文本控件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;
2、length是确定字符串长度啊!!!
一个汉字长度应该是2,而一个字母长度是1,这不同于字符个数的概念。
字符的个数必须自己写一个函数进行判断字符的高位字节是否大于160!如果是的话,就是一个汉字,就把两个算一个!
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;
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;