What you're seeing probably aren't null characters. They're probably just the upper eight bits of a character with a code-point value less than 256.
If you really do have null characters in your string that aren't supposed to be there, the first thing you should do is figure out how they're getting there. There's probably a bug in your program if they're there when they shouldn't be.
If the code that generates the string is bug-free and you still have unwanted null characters, then you can remove them fairly easily. The common way to remove stuff from a string is with the Delete
standard function. You can specify any character by its numeric value with the #
syntax, and the compiler can usually figure out whether it needs to represent an AnsiChar or a WideChar.
procedure RemoveNullCharacters(var s: WideString);
var
i: Integer;
begin
i := 1;
while i < Length(s) do
if s[i] = #0 then
Delete(s, i, 1)
else
Inc(i);
end;
But that may re-allocate the string many times (once for each null character). To avoid that, you can pack the string in-place:
procedure RemoveNullCharacters(var s: WideString);
var
i, j: Integer;
begin
j := 0;
for i := 1 to Length(s) do
if s[i] <> #0 then begin
Inc(j);
s[j] := s[i];
end;
if j < Length(s) then
SetLength(s, j);
end;
Those functions will work for any of Delphi's string types; just change the parameter type.