Ron
2008-07-29 16:47:54
Hoorah! This is a sweet function if anyone is into parsing strings/text or for anyone who is sick of the lack of functionality with the pos function.
Function code:
Test function:
~Ron
Function code:
// Will find the next position of a substr inside a str, after X
// number of substrs. OR set AfterFirst to -1 to find the last pos.
// Made by Ron :)
function NextPos(Substr, Str : String; AfterFirst : Integer) : Integer;
var
i, Pos1, TotalPos : Integer;
begin
Pos1 := 0;
i := -1;
repeat
inc(i);
Pos1 := Pos(Substr, Str);
if (Pos1 = 0) then
Break;
Str := Copy(Str, Pos1 + 1, Length(Str));
TotalPos := TotalPos + Pos1;
WriteLn(IntToStr(TotalPos));
until (i >= AfterFirst) and (AfterFirst > -1);
Result := TotalPos;
end;
Test function:
program NextPosFunctionTestByRon;
const
Substr = 'holy';
Str = 'Have you read the holy book? Holy shit! No not the holy bible! The holy hotties book.';
AfterFirst = 1;
CaseSens = False;
// Will find the next position of a substr inside a str, after X
// number of substrs. OR set AfterFirst to -1 to find the last pos.
// Made by Ron :)
function NextPos(Substr, Str : String; AfterFirst : Integer; CaseSens : Boolean) : Integer;
var
i, Pos1, TotalPos : Integer;
begin
if not(CaseSens) then
begin
Substr := Lowercase(Substr);
Str := Lowercase(Str);
end;
Pos1 := 0;
i := -1;
repeat
inc(i);
Pos1 := Pos(Substr, Str);
if (Pos1 = 0) then
Break;
Str := Copy(Str, Pos1 + 1, Length(Str));
TotalPos := TotalPos + Pos1;
WriteLn(IntToStr(TotalPos));
until (i >= AfterFirst) and (AfterFirst > -1);
Result := TotalPos;
end;
begin
ClearDebug;
WriteLn(IntToStr(NextPos(Substr, Str, AfterFirst, CaseSens)));
end.
~Ron