Delphi 如何查看Memo中的某个字有多少个呢?

来源:百度知道 编辑:UC知道 时间:2024/06/02 03:24:42
比如我让程序判断Memo文本里有多少个“你”字
然后在label中显示出来多少个.
该怎样做呢? 最好给个代码 !
新手初学delphi,大家帮帮忙啦!

label1.Caption:=inttostr(length(trim(memo1.Text)));

其中里面的 trim() 是删除首位的空格

记得要uses StrUtils;

procedure TForm1.BitBtn1Click(Sender: TObject);
Var
i,m,k:integer;
astr:string;
begin
astr:='你';
k:=0; //记数
m:=0; //临时变量,记录每次找到这个字(如:你)的索引值

for i:=1 to Length(self.Memo1.Lines.Text) do
begin
{}if i<=m then continue;
{}m:=StrUtils.PosEx(astr,self.Memo1.Lines.Text,i);
{}if m>0 then inc(k);
end;
self.Label1.Caption:=inttostr(k);