In a recent application that involved receiving strings over a serial link I found myself writing code like:
if (pos('needle', haystack) = 1) then ...
in order to check if a particular substring is at the begging of a string.
It struck me that the pos function is not ideal for this as it has no idea which location I'm looking for the substring to be in.
Is there a good function that does this?
Is there a more generalised function like IsSubStringAt(needle, haystack, position)
?
I did think of using something like this:
function IsSubstrAt(const needle, haystack: string; position: Integer): Boolean;
var
ii: integer;
begin
result := true;
for ii := 1 to length(needle) de begin
if (haystack[poition + ii -1] <> needle[ii]) then begin
result := false;
break;
end;
end;
end;
with some error checking.
I was hoping to find a ready rolled answer.