字符串"sdfadsfasfsd"用什么函数判断有几个"a"

解决方案 »

  1.   

    function StrSubCount(const Source,Sub:string):integer;
    var Buf:string;
        Len,i:integer;
    begin
       Result:=0;
       Buf:=Source;
       i:=Pos(Sub, Buf);
       Len:=Length(Sub);
      while i <> 0 do
        begin
        Inc(Result);
        Delete(Buf,1,i+Len-1);
        i:=Pos(Sub,Buf);
      end;
    end;
      

  2.   

    另一简单方法:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      t:tstringlist;
    begin
       t:=tstringlist.Create;
       t.Delimiter:='a';
       t.DelimitedText:='sdfadsfasfsd';
       ShowMessage(inttostr(t.count-1));  // t.count-1就是a的个数
       t.free;
    end;