I'm trying to create a function that is similar to Delphi's pos function, but that i could pass different strings to be searched, instead of only one. So i could call the function like this :
multipos('word1#word2#word3','this is a sample text with word2',false); // will return 'word2' The function would return which string was found.
The code i did is below and it's working but it's too slow. How could i improve the speed of this code ?
function multipos(needles,key: string; requireAll: boolean): string; var k: array [1 .. 50] of string; i, j: integer; r, aux: string; flag: boolean; begin if trim(key) = '' then Result := '' else try r := ''; Result := ''; j := 1; for i := 1 to 50 do k[i] := ''; for i := 1 to length(needles) do begin if needles[i] <> '#' then aux := aux + needles[i] else begin k[j] := aux; Inc(j); aux := ''; end; if j >= 50 then break; end; if aux <> '' then k[j] := aux; for i := 1 to j do begin if k[i] = '' then break else if pos(lowercase(k[i]), lowercase(key)) > 0 then begin if not requireAll then begin Result := k[i]; break; end else begin r := r + k[i] + ','; flag := i = j; if not flag then flag := k[i + 1] = ''; if flag then begin Result := r; end; end; end else if requireAll then begin break; end; end; except on e: exception do begin Result := ''; end; end; end;