AI摘要
这篇文章提供了一个Delphi代码示例,用于统计字符串中的字符和汉字数量。代码通过遍历字符串,检查每个字符的ASCII值来区分字符和汉字,并使用两个变量e和c分别计数。最后,将计数结果显示在两个标签上。
这篇文章提供了一个Delphi代码示例,用于统计字符串中的字符和汉字数量。代码通过遍
delphi代码
- procedure TForm1.btn1Click(Sender: TObject);
- var i,e,c:integer;
- s:string;
- begin
- s:=mmo1.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;