[fpc-pascal]feature request: enhanced "pos"
Jeff Pohlmeyer
yetanothergeek at yahoo.com
Sat Mar 6 07:56:44 CET 2004
> Dunno if this is the right place to post such a message, but here goes...
> I would love to see a "pos" function that takes an additional parameter, being
> the position in the string where it should START scanning. e.g.:
> my_str = 'Hello, everybody... Hello, world!';
> x := pos ('Hello', my_str, 6);
> So it starts scanning at position 6 (after the first Hello) and finds the
> second Hello at position 21; returns 21. (not 21-6)
> Passing it a starting position 2 (instead of 6) would also skip the first
> Hello, and return the same 21.
> (( Of course, passing it a starting position greater than the length(str) -
> length(target) should return 0; also it should return 0 if the target is not found
> between startpos and the end of the string. ))
Dunno if this is the right place to post such an answer, but here goes...
program postest;
{$H+}{$MODE OBJFPC}
uses strings;
function PosFrom( const needle:AnsiString; const haystack:AnsiString;
StartPt:LongInt):LongInt;
var
p:pChar;
begin
Result:=0;
if (StartPt <= Length(haystack))
then begin
p:=StrPos(pChar(@haystack[StartPt]), pChar(needle));
if ( p <> nil ) then Result:= ( p - @haystack[1] ) + 1;
end
end;
// Testing ...
var
subject, pattern : string;
i:integer;
begin
subject := 'Hello, everybody... Hello, world!';
pattern := 'Hello';
for i:=1 to length(subject) do
WriteLn('Position from ', i, ' is ', PosFrom(pattern, subject, i));
end.
__________________________________
Do you Yahoo!?
Yahoo! Search - Find what youre looking for faster
http://search.yahoo.com
More information about the fpc-pascal
mailing list