[Lazarus] Getting and setting the x numbered word in a sentence

Howard Page-Clark hdpc at talktalk.net
Tue Jan 7 20:19:37 CET 2014


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;





More information about the Lazarus mailing list