function CharCount(Const ss:String;Ch:Char):integer; var i:integer; begin ReSult:=0; for i:=1 to Length(ss) do if ss[i]=Ch then inc(ReSult);end;
var sub, source: String; Count : Integer; begin source := 'asdsdfasdfgerdfrt'; sub := 'as'; Count := (Length(source) - Length(StringReplace(source, sub, '',[rfReplaceAll]))) div Length(sub); ShowMessage(IntToStr(Count)); end;
多谢了,我刚接触delphi一周,要学的东西还很多。
function strCount( subs:String;s:Char):integer;begin ReSult:=0; while pos(subs,s)<>0 then begin result:=result+1; s:=copy(s,pos(subs,s)+length(subs),1000); end; end;
var i:integer;
begin
ReSult:=0;
for i:=1 to Length(ss) do
if ss[i]=Ch then
inc(ReSult);end;
sub, source: String;
Count : Integer;
begin
source := 'asdsdfasdfgerdfrt';
sub := 'as';
Count := (Length(source) - Length(StringReplace(source, sub, '',[rfReplaceAll]))) div Length(sub);
ShowMessage(IntToStr(Count));
end;
ReSult:=0;
while pos(subs,s)<>0 then
begin
result:=result+1;
s:=copy(s,pos(subs,s)+length(subs),1000);
end;
end;