Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {==============================================================================]
- Explanation: Returns all the s positions in str.
- If overlap is set to true, strings can overlap.
- ('aa', 'baaaah', False) => [2,3,4]
- ('aa', 'baaaah', True) => [2,4]
- [==============================================================================}
- function PosAll(s, str: string; overlap: Boolean): TIntArray;
- var
- sL, strL, o, p, r: Integer;
- begin
- sL := Length(s);
- strL := Length(str);
- if (sL <= strL) then
- begin
- if not overlap then
- begin
- o := Length(s);
- p := (p - (o - 1));
- end else
- o := 1;
- SetLength(Result, ((strL div sL) + 1));
- repeat
- p := PosEx(s, str, (p + o));
- if (p > 0) then
- begin
- Result[r] := p;
- Inc(r);
- end;
- until (p <= 0);
- end;
- SetLength(Result, r);
- end;
- var
- s, str: string;
- begin
- ClearDebug;
- str := '|||| PosAll() Test ||| *** ||| Should work pretty well. ||||';
- s := '||';
- WriteLn('PosAll(s, str, True): ' + TIAToStr(PosAll('||', str, True)));
- WriteLn('PosAll(s, str, False): ' + TIAToStr(PosAll('||', str, False)));
- end.
Advertisement
Add Comment
Please, Sign In to add comment