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"
You could say replace word 3 with string "stood" so the result would be a string which was " the cat stood on the mat" even though you didn't know that the 3rd word was originally "sat"?
Hope that makes sense :)
Thanks
Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140107/3cc67f99/attachment.html>
Mailing lists / lazarus / 2014-01
Getting and setting the x numbered word in a sentence
4 messages · 4 people · –
- HHoward Page-Clark 1
- JJohn Landmesser 1
- KK. P. 1
- RRichard Mace 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Not exactly - but it is trivially simple to write your own using
http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/extractwordpos.html
Cheers,
Kai
Date: Tue, 7 Jan 2014 18:04:53 +0000
From:
To:
Subject: [Lazarus] Getting and setting the x numbered word in a sentence
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"
You could say replace word 3 with string "stood" so the result would be a string which was " the cat stood on the mat" even though you didn't know that the 3rd word was originally "sat"?
Hope that makes sense :)
Thanks
Richard
Is there an existing routine in Lazarus that will get and set effectively the x numbered…
Richard Mace wrote 5 lines
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;Is there an existing routine in Lazarus that will get and set effectively the x numbered…
Richard Mace wrote 13 lines
Have a look at functions in StrUtils like:
function WordCount(const S: string; const WordDelims: TSysCharSet): Integer;
function WordPosition(const N: Integer; const S: string; const
WordDelims: TSysCharSet): Integer;
function ExtractWord(N: Integer; const S: string; const WordDelims:
TSysCharSet): string;inline;
function ExtractWordPos(N: Integer; const S: string; const WordDelims:
TSysCharSet; var Pos: Integer): string;