How do I remove a null from a string?
- This should be simple but apparently it's not. I have a text file, 694 bytes. At byte 500 there's a null in the file. I've tried reading it as text and binary and I end up with a string, 694 bytes long. But anyting I do with this string fails because it appears to stop at character 500. I can do a ? asc(mystring.substring(549,1)) and I get 9 (tab). Print character 550 and I get 0. Print character 551 and I get 32 (space). How do I get rid of this null? I've tried:
sBuffer.Replace(0, 32)
sBuffer.Replace(chr(0), Chr(32))
sBuffer.Replace(vbNull, Chr(32))
Nothing seems to work. How do I get rid of this null from the string?
Thanks!
Answers
- String functions will not be coerced to do that, since Chr(0) is not a legal character...its the string terminator, but you already knew that.
Your numbers do not make sense to me, but something like sBuffer(550) = 32 should fix your problem. The trouble with the way that you are doing it is that you are trying to elicit VB's help to fix something that VB cannot fix, or tolerate...the file "looks" like at least two strings.
I think that the type of sBuffer is relevant to your dilemma. It should probably be a byte buffer, and you should use binary reading methods, imo.- Marked As Answer byJeff ShanMSFT, ModeratorFriday, November 06, 2009 8:50 AM
- Read your file as binary bytes, replace the 0 and write the bytes to the file.
- Marked As Answer byJeff ShanMSFT, ModeratorFriday, November 06, 2009 8:50 AM
All Replies
- Hmmm...
I would have thought this would have worked:
sBuffer = sBuffer.Replace(chr(0),Chr(32))
What happens when you try this? Anything? An Error?
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Thanks for the reply...
It does nothing. No error and the null is still there. - String functions will not be coerced to do that, since Chr(0) is not a legal character...its the string terminator, but you already knew that.
Your numbers do not make sense to me, but something like sBuffer(550) = 32 should fix your problem. The trouble with the way that you are doing it is that you are trying to elicit VB's help to fix something that VB cannot fix, or tolerate...the file "looks" like at least two strings.
I think that the type of sBuffer is relevant to your dilemma. It should probably be a byte buffer, and you should use binary reading methods, imo.- Marked As Answer byJeff ShanMSFT, ModeratorFriday, November 06, 2009 8:50 AM
- Read your file as binary bytes, replace the 0 and write the bytes to the file.
- Marked As Answer byJeff ShanMSFT, ModeratorFriday, November 06, 2009 8:50 AM
- That did it, read as binary, loop through and replace 0 with 32. Works like a charm, thanks!


