var s:string; WordCount:integer; begin s:=Memo1.Text; WordCount:=0; while s<>'' do begin inc(WordCount); if ord(s[1])<=127 then Delete(s,1,1) else Delete(s,1,2); end; Edit1.Text:=IntToStr(WordCount); end;//回车也有两个\r\n
procedure TForm1.Button1Click(Sender: TObject); var i,j,m,n:integer; s:string; begin m:=0; n:=0; s:=form1.Memo1.Text; i:=length(s); if i>0 then begin for j:=1 to i do begin if ord(s[j])>128 then begin m:=m+1; end else n:=n+1; end; end; showmessage('文字:'+inttostr(m div 2)+' '+'字母、数字等:'+inttostr(n)); end;
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons;type TForm1 = class(TForm) BitBtn1: TBitBtn; procedure BitBtn1Click(Sender: TObject); private { Private declarations } procedure fetchNum(str: string; var hnum, ynum: Integer); public { Public declarations } end;var Form1: TForm1; s: string;implementation{$R *.dfm}procedure TForm1.fetchNum(str: string; var hnum, ynum: Integer); var I, Iend: Integer; begin Iend := length(str); I := 1; hnum := 0; ynum := 0; while I <= Iend do begin if ord(str[I]) < 128 then begin inc(ynum); inc(I) end else begin inc(hnum); inc(I, 2) end; end; end;procedure TForm1.BitBtn1Click(Sender: TObject); var hnum, ynum: Integer; begin s := '我df的d祖国'; fetchNum(s, hnum, ynum); showmessage('汉字:' + inttostr(hnum) + '个;字母:' + inttostr(ynum) + '个') end;end. 显示 汉字:4个;字母:3个
var WS:WideString; begin WS:=Memo1.Text; ShowMessage(IntToStr(Length(WS)));//回车计算在内. end;
s:string;
WordCount:integer;
begin
s:=Memo1.Text;
WordCount:=0;
while s<>'' do
begin
inc(WordCount);
if ord(s[1])<=127 then
Delete(s,1,1)
else
Delete(s,1,2);
end;
Edit1.Text:=IntToStr(WordCount);
end;//回车也有两个\r\n
var
i,j,m,n:integer;
s:string;
begin
m:=0;
n:=0;
s:=form1.Memo1.Text;
i:=length(s);
if i>0 then
begin
for j:=1 to i do
begin
if ord(s[j])>128 then
begin
m:=m+1;
end
else
n:=n+1;
end;
end;
showmessage('文字:'+inttostr(m div 2)+' '+'字母、数字等:'+inttostr(n));
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
procedure fetchNum(str: string; var hnum, ynum: Integer);
public
{ Public declarations }
end;var
Form1: TForm1;
s: string;implementation{$R *.dfm}procedure TForm1.fetchNum(str: string; var hnum, ynum: Integer);
var
I, Iend: Integer;
begin
Iend := length(str);
I := 1;
hnum := 0;
ynum := 0;
while I <= Iend do
begin
if ord(str[I]) < 128 then
begin
inc(ynum);
inc(I)
end
else
begin
inc(hnum);
inc(I, 2)
end;
end;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var
hnum, ynum: Integer;
begin
s := '我df的d祖国';
fetchNum(s, hnum, ynum);
showmessage('汉字:' + inttostr(hnum) + '个;字母:' + inttostr(ynum) + '个')
end;end.
显示 汉字:4个;字母:3个
WS:WideString;
begin
WS:=Memo1.Text;
ShowMessage(IntToStr(Length(WS)));//回车计算在内.
end;
应该改为:
i := 0;