4

I have a very simple .NET program. It's just to write a string to the textbox. There is a strange character appearing at the end of my string.

enter image description here

This happens only on my 32-bit XP box. The same program works fine on another 64bit Windows 2008 machine.

The program is as simple as this.

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Text = "Hello\n\0\0\0\0\0\0";
    }

I know it's odd to add \0 at the end of the string and I can trim them before applying to the textbox. The textbox is set to allow multi-line.

Just out of curiousity, does anybody know where the problem comes from? Both machines got .NET 3.5 SP1 installed. Both of them are set to have the same regional settings. I doubt if it is related to 32-bit or 64-bit.

UPDATE

Thanks to @DBM and @Andrew. The strange character is coming from \n but nothing to do with \0. Now, it sounds like Windows 2008 can understand both \r\n and \n. Can anybody confirm that?

Arpit
  • 6,212
  • 8
  • 38
  • 69
Harvey Kwok
  • 11,713
  • 6
  • 37
  • 59

2 Answers2

6

Standard end-of-line sequence in Windows is \r\n. The text box isn't recognising the \n as a new-line without the preceding carriage return (\r).

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Got it. I think you are right that I should use \r\n instead. It sounds like Windows 2008 just happens to understand \n but XP doesn't. Can anyone confirm that Windows 2008 support \n? – Harvey Kwok Jun 08 '11 at 01:21
5

You should use Environment.NewLine instead of \r\n, in general.

In the way of explanation: Environment.NewLine will contain the proper sequence of escape characters for whatever platform the application is running in. On Windows, it's \r\n, but *nix (if I recall correctly) uses \n only.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • Thanks, I agree with you that I should use `Environment.NewLine` too. I think my intention for the question was to find out why they behave differently in Windows 2008 and Windows XP rather than making the strange character disappeared. – Harvey Kwok Jun 08 '11 at 01:25