Visual Basic > Visual Basic Forums > Visual Basic Language > How do I remove a null from a string?
Ask a questionAsk a question
 

AnswerHow do I remove a null from a string?

  • Wednesday, November 04, 2009 3:51 PMtrwilson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Wednesday, November 04, 2009 4:13 PMjinzai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Wednesday, November 04, 2009 4:50 PMJohnWein Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Read your file as binary bytes, replace the 0 and write the bytes to the file.

All Replies

  • Wednesday, November 04, 2009 4:06 PMDeborahKMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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!
  • Wednesday, November 04, 2009 4:13 PMtrwilson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for the reply...

    It does nothing.  No error and the null is still there.
  • Wednesday, November 04, 2009 4:13 PMjinzai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Wednesday, November 04, 2009 4:50 PMJohnWein Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Read your file as binary bytes, replace the 0 and write the bytes to the file.
  • Wednesday, November 04, 2009 6:45 PMtrwilson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    That did it, read as binary, loop through and replace 0 with 32.  Works like a charm, thanks!