Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>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.</p> <p>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 <a href="http://docwiki.embarcadero.com/VCL/en/System.Delete" rel="noreferrer"><code>Delete</code></a> standard function. You can specify any character by its numeric value with the <code>#</code> syntax, and the compiler can usually figure out whether it needs to represent an AnsiChar or a WideChar.</p> <pre><code>procedure RemoveNullCharacters(var s: WideString); var i: Integer; begin i := 1; while i &lt; Length(s) do if s[i] = #0 then Delete(s, i, 1) else Inc(i); end; </code></pre> <p>But that may re-allocate the string many times (once for each null character). To avoid that, you can pack the string in-place:</p> <pre><code>procedure RemoveNullCharacters(var s: WideString); var i, j: Integer; begin j := 0; for i := 1 to Length(s) do if s[i] &lt;&gt; #0 then begin Inc(j); s[j] := s[i]; end; if j &lt; Length(s) then SetLength(s, j); end; </code></pre> <p>Those functions will work for any of Delphi's string types; just change the parameter type.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload