views:

274

answers:

1

How can I search a dynamic array of char in Delphi 6 for a sub-string and get back an index to a match, not a pointer? I've seen functions in Delphi 6 that do this for strings but not for dynamic char arrays. There is a function called SearchBuf but that function returns a PChar pointer to the match location when what I need is the array index of the match.

Thanks.

+1  A: 

If you have a pointer to the match, simply subtract the pointer to the first character, and you'll have your index.

var
  Buf, Result: PChar;
  Index: Integer;

Result := SearchBuf(Buf, ...);
if Assigned(Result) then
  Index := Result - Buf
else
  Index := -1; // not found

I'm pretty sure that pointer arithmetic is allowed in Delphi 6. If not, then type-cast the pointers to integral types first:

Index := Cardinal(Result) - Cardinal(Buf);
Rob Kennedy