I have a WideString variable containing some data but when the string was assigned some extra nulls where added at more or less random places in the data. I now need to strip these nulls out of the variable. If it had been a string I would have checked each Char to see if Char(x) = 0 but as this is a WideString I dont think this work? How can I best strip these out?
I’m using Delphi 2006
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
Deletestandard 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.But that may re-allocate the string many times (once for each null character). To avoid that, you can pack the string in-place:
Those functions will work for any of Delphi’s string types; just change the parameter type.