怎样从一个字符串获取某个子串的后面的字符串,且这个子串不在第一位?
例如字符串为abcdabe,子串为ab,则获取e(因为abcdabe开头为ab,所以截取第二个ab后面的);
字符串为abbcc,子串为bb,则获取cc

解决方案 »

  1.   

    粗略的写了一下!
    procedure TForm1.Button1Click(Sender: TObject);
    var
      sub,str: string;
      Index: Integer;
    begin
      sub := 'bb';
      str := 'abbcc';
      Index := Pos(sub,copy(str,2,Length(str)-1));
      ShowMessage(Copy(str,Index+1+Length(sub),Length(str)-Index));
    end;
      

  2.   

    //uses StrUtils单元
      
    function GetSubStr(const S, Sub: string): string;
       var
          iOffset: Integer;
          iPos: Integer;
          iSubLen: Integer;
       begin
          iOffSet := 0;
          iSubLen := Length(Sub);
          iPos := Pos(Sub, S);
          while iPos > 0 do
          begin
             iOffSet := iPos + iSubLen;
             iPos := PosEx(Sub, S, iOffSet);
          end;
          Result := Copy(S, iOffSet, Length(S) - iOffSet + 1);
       end;
      

  3.   

    function GetRight(abcdabe: string; ab: string): string;
    var
    idx: integer;
    begin
      abcdabe := 'abcdabe';
      ab := 'ab';
      idx := Pos(pchar(@abcdabe[2]), ab);
      if (idx>0) then
      begin
        idx := idx + Length(ab) + 1;
        result := Copy(abcdabe, idx, Length(abcdabe)-idx+1);
      end;
    end;
      

  4.   

    Pos()参数写反了,应该是
    idx := Pos(ab pchar(@abcdabe[2]));
      

  5.   

    function GetSubStr1(const S, Sub: string): string;
       var
          iPos: Integer;
          iSubLen: Integer;
       begin
          Result := S;
          iSubLen := Length(Sub);
          iPos := Pos(Sub, Result);
          while iPos > 0 do
          begin
             Delete(Result, 1, iPos + iSubLen-1);
             iPos := Pos(Sub, Result);
          end;
       end;