我现在有这样一个问题:有一字符串 
str:='cdabedauaakul'
我想找这个字符串中 'a' 第三次出现的位置(或第N次出现的位置),不知道DELPHI中有这样的函数没有,如没有应该怎么写这个函数? 

解决方案 »

  1.   

    str2:=copy(str,pos('a',str)+1,length(str)-pos('a',str));
    pos('a',str2);
    我是这样解决的,其实也不麻烦
      

  2.   

    //参考如下代码~~
    function PosEx(mSubstr: string; mStr: string; mCount: Integer): Integer;
    var
      I: Integer;
      S: string;
    begin
      Result := 0;
      if (mStr = '') or (mSubstr = '') or (mCount = 0) then Exit;
      S := mSubstr;
      S[1] := Chr(not Ord(S[1]));
      for I := 1 to mCount - 1 do mStr := StringReplace(mStr, mSubstr, S, []);
      Result := Pos(mSubstr, mStr);
    end; { PosEx }procedure TForm1.Button1Click(Sender: TObject);
    begin
      Caption := IntToStr(PosEx(Edit1.Text, Edit2.Text, SpinEdit1.Value));
    end;
      

  3.   

    可以自己写一个函数:function A(s: string; c: char; m: integer): integer;
    其中s代表要搜索的字符串,c代表要搜索的字符,m代表c出现的次数。
    Function A(s: string; c: char; m: integer);
    var
     i: integer;
     n: integer;
    begin
     n:=0;
     for i:=1 to length(s) do
     begin
      if s[i]=c then
       Inc(n);
      if n=m then
       break;
     end;
     result:=i;
    end;