views:

114

answers:

1
+1  A: 

You may add an additional argument to the function:

function GetAWord(sentence, word1, word2 : string; Index: Integer) : string;
var
  N: integer;

begin
  repeat
    N:= pos(word1, sentence);
    if N = 0 then begin
      result := '';
      exit;
    end;
    delete(sentence, 1, n + length(word1) - 1);
    n := pos(word2, sentence);
    if n = 0 then begin
      result := '';
      exit;
    end;
    Dec(Index);
    if Index < 0 then begin
      result := copy(sentence, 1, n - 1);
      Exit
    end;
    delete(sentence, 1, n + length(word2) - 1);
  until False;
end;


// test
procedure TForm1.Button1Click(Sender: TObject);
const
  S = '115552211666221177722';

begin
  ShowMessage(GetAWord(S, '11', '22', 0));
  ShowMessage(GetAWord(S, '11', '22', 1));
  ShowMessage(GetAWord(S, '11', '22', 2));
  ShowMessage(GetAWord(S, '11', '22', 4));
end;

Well you can find all entries in a single function:

procedure ParseSentence(sentence, word1, word2 : string; Strings: TStrings);
var
  N: integer;

begin
  Strings.Clear;
  repeat
    N:= pos(word1, sentence);
    if N = 0 then exit;
    delete(sentence, 1, n + length(word1) - 1);
    n := pos(word2, sentence);
    if n = 0 then exit;
    Strings.Add(copy(sentence, 1, n - 1));
    delete(sentence, 1, n + length(word2) - 1);
  until False;
end;

procedure TForm1.Button2Click(Sender: TObject);
const
  S = '115552211666221177722';

var
  SL: TStringList;

begin
  SL:= TStringList.Create;
  ParseSentence(S, '11', '22', SL);
  Memo1.Lines.Assign(SL);
  SL.Free;
end;
Serg
Thanks again for the help but I am not sure how this might help if I did not know how many words were in the original string that needed to be indexed. I am pretty new to programming (obviously) so that's why I ask if this would work with any string even if I do not know how many indexes of the keywords there are in the string ?
emily
Thanks, that did the trick (both actully work) but the 2nd one is better for my needs. Thanks again
emily