On 07/01/2014 18:04, Richard Mace wrote:
Hi,
Is there an existing routine in Lazarus that will get and set
effectively the x numbered word in a sentence without actually knowing
what the actual word is?
E.g with the string "the cat sat on the mat"

If word delimiters are consistently all the same character (as in your example) you can press a stringlist into service:

function ReplaceNthWord(const aPhrase: string; const aReplacement: string; aWordIndex: integer): string;

var sl: TStringList;

begin
    Result:='';
    if Length(aPhrase)=0 then Exit;
    sl:=TStringList.Create;
    try
      sl.Delimiter:=' ';
      sl.DelimitedText:=aPhrase;
      if (aWordIndex > 0) and (aWordIndex <= sl.Count) then
        sl[Pred(aWordIndex)]:=aReplacement;
      Result:=sl.DelimitedText;
    finally
      sl.Free;
    end;
end;


--
_______________________________________________
Lazarus mailing list
[email protected]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply via email to