I am sorry you don't get it..
POS for a pchar has no overload so what it is doing it first recreating an Ansistring using the NULL (#0) as the end of line marker instead..
It should not be doing that for Pchar(S) etc..
It should be doing what StrLen does.. and That is return the position of the Null..
You are wrong. The purpose of
StrLen is to determine the length of the passed
PChar. That just happens to coincide with the index of the terminating
NUL-character.
Your
PChar is indeed converted to an
AnsiString due to there not being an overload for
PChar. And this means that the resulting
AnsiString will contain
StrLen characters which is to mean the
PChar without the terminating
NUL-character. Thus
Pos will never find that terminating
NUL, because that
NUL is never
inside the string.
If you're dealing with
PChars then please use the functions for
PChars (namely
StrPos in this case).
Allow me to add a failing example that contradicts some other comments about #0 not being in the string is why this is..
Var
S:String;
begin
S := 'Test'+#0;
Caption := Pos(#0, Pchar(S)); //Fails;
However this does work ..
Caption := POS(#0, S);
That doesn't contradict anything that was said. The layout of the string
S is
Test#0#0. The first
NUL is part of the string (
Length(S) will return
5), the second
NUL is not part of the string as that is the terminator. Now you cast that string to
PChar, that means that the
PChar will essentially be
Test#0 with
StrLen(S) returning
4 and that remaining
NUL-character being the terminator. Now if you pass that to
Pos that will - as mentioned above - be converted to an
AnsiString, but this time with the layout
Test#0 where
Length will be
4 and the
NUL-character will not be part of the string.
If you're mixing Pascal and
NUL-terminated you need to know what you're doing! This is all by design.