Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

string.toString question

Say I have a string containing some control characters (tabs, bells etc). At runtime, if I use toString() on this string, does it return the exact same character sequence? Or does it replace the control characters with escape sequences?

Sorry to ask, but I don't have access to a running .net installation at the moment, and MSDN isn't clear.

Bill Rayer
Monday, August 04, 2003

"does it return the exact same character sequence?" -- Yes
  string s1 = "Banana";
  string s2 = s1.ToString();
  Console.WriteLine( Object.ReferenceEquals(s1,s2) );
  // Prints "True"

"does it replace the control characters with escape sequences?" -- No

Duncan Smart
Monday, August 04, 2003

But what if s1 contains control characters that can't be represented at compile time, eg:

  string s1 = "";
  string s2 = "";
  s1 = Chr(1) + Chr(2) + Chr(10) + Chr(13);
  s2 = s1.ToString();

(Chr is meant to return a string consisting of a single character with the specified value although I may have got the syntax wrong).

Will s1 and s2 contain the same character sequence? Or is s2 escaped?

Bill Rayer
Monday, August 04, 2003


It's won't escape the string because it doesn't need to. 

Escaping is only there to help humans because it is quite hard for us to type ascii char 13 on a keyboard.

RJ
Tuesday, August 05, 2003

Many thanks for your answer - that is a great help.

Bill Rayer
Tuesday, August 05, 2003

*  Recent Topics

*  Fog Creek Home