MessageBox doesn't show all given string

I have a c# program and am trying to call a messageBox with a specific string(normal string, nothing special), And when reaching a variable to concatenate with the string, it apparently stops the concatenation. The code:

string first = userInfo.info.getFirst(); //Some function
string last = userInfo.info.getLast(); // Some function
string message = first + "_" + last + " !";
MessageBox.Show(message);
// Output will be "(first value)";

I did try to debug and the values of "first" and "last" are correct and fine. I also analyzed to see if any CPU or Memory peak occur(using VS's tools), but seen none.

Any idea as for the problem? Thanks a lot!

Jon Skeet
people
quotationmark

The Win32 GUI libraries terminate strings if they find a \0 character (U+0000, Unicode "null") in them. For example, if you had:

MessageBox.Show("First part\0Second part");

then only First part would be displayed.

There are at least two options here:

  • Work out where the "bad" character is coming from. It's often a misuse of the Stream or TextReader API, not paying attention to how many bytes or characters are returned by a Read call
  • Just remove the "bad" character, e.g. message = message.Replace("\0", "");

The first option is preferable - I'd only resort to the second if I really couldn't get clean data.

people

See more on this question at Stackoverflow