string.Format encoding?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(myFormat);

....that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

....load xmlNode WorkNode...
string myFormat= WorkNode.Attributes["text"].InnerText.ToString();
string formattedString = string.Format(myFormat);

....the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?
 
Scewbedew,

That's the ting, it isn't the same format string. The same string in an
XML file would have the character code 10 (newline), not "\n" in it. You
need to insert the newline character into your XML, not use "\n".

Hope this helps.
 
Oh, I thought that string formatting was done by string.Format...silly me ;-)

I can't change the xml file, so I guess I'd have to do some subsititutions
of my own. Thanks for the info.

Nicholas Paldino said:
Scewbedew,

That's the ting, it isn't the same format string. The same string in an
XML file would have the character code 10 (newline), not "\n" in it. You
need to insert the newline character into your XML, not use "\n".

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Scewbedew said:
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(myFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attributes["text"].InnerText.ToString();
string formattedString = string.Format(myFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters
correctly?
 
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(myFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attributes["text"].InnerText.ToString();
string formattedString = string.Format(myFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?
Interesting. Did you use the debugger to look at the string you got from the
xml file to verify that you actually got what you expected?
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Scewbedew said:
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(myFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attributes["text"].InnerText.ToString();
string formattedString = string.Format(myFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?

Probably a typo, but you posted "/n" instead of "\n". However, the more
important point is that you misunderstand who is doing what.

string.Format is not resolving \n into a newline character. The
compiler is doing that. In other words, the variable strFormat does not
contain:

L-i-n-e-1-\-n-L-i-n-e-2

In fact, it contains this:

L-i-n-e-1-newline-L-i-n-e-2

because the compiler has already resolved the \n notation into the
corresponding control character. So, string.Format doesn't "handle" the
\n notation at all. It just treats the newline like any other character
and puts it in the output string.

Now, when you read "Line1\nLine2" from an XML file, what you're passing
to string.Format is exactly that sequence of characters:

L-i-n-e-1-\-n-L-i-n-e-2

and so that's what it puts in the output string.

So, the real question is how can you take a character string that may
contain some character escapes, and translate them into the appropriate
control characters. Perhaps someone else can answer that one.
 
Yes. In fact, I discovered exactly what Bruce Wood describes in another
answer to my question: the xml originated string included all the characters
unchanged, while the inline string included newline charactes instead of the
"\n" characters.

Otis Mukinfus said:
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(myFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attributes["text"].InnerText.ToString();
string formattedString = string.Format(myFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?
Interesting. Did you use the debugger to look at the string you got from the
xml file to verify that you actually got what you expected?
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Thanks for the clarification. You where correct about the typo, sorry about
that. In my mind, it was sooo very obvious that string.Format should handle
the formatting of the string, so I got really puzzled when it didn't happen.

I have verified exactly the behaviour you describe while debugging my app,
so it is now painfully obvious that my initial assumption was wrong (or...as
a matter of fact I'm never wrong, it's only the world that's temporary out of
sync...)

If anyone has any good input to how to make the character translation I'm
eager to take part of it.

Bruce Wood said:
Suppose I have the following code:

string myFormat = "Line1/nLine 2";
string formattedString = string.Format(myFormat);

...that would produce a 2-line output as expected.

But if I load that very same format string from an xml file:

...load xmlNode WorkNode...
string myFormat= WorkNode.Attributes["text"].InnerText.ToString();
string formattedString = string.Format(myFormat);

...the result is a one-line "Line1/nLine 2" output.

How can I get the string.Format method to handle control characters correctly?

Probably a typo, but you posted "/n" instead of "\n". However, the more
important point is that you misunderstand who is doing what.

string.Format is not resolving \n into a newline character. The
compiler is doing that. In other words, the variable strFormat does not
contain:

L-i-n-e-1-\-n-L-i-n-e-2

In fact, it contains this:

L-i-n-e-1-newline-L-i-n-e-2

because the compiler has already resolved the \n notation into the
corresponding control character. So, string.Format doesn't "handle" the
\n notation at all. It just treats the newline like any other character
and puts it in the output string.

Now, when you read "Line1\nLine2" from an XML file, what you're passing
to string.Format is exactly that sequence of characters:

L-i-n-e-1-\-n-L-i-n-e-2

and so that's what it puts in the output string.

So, the real question is how can you take a character string that may
contain some character escapes, and translate them into the appropriate
control characters. Perhaps someone else can answer that one.
 
Back
Top