By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,423 Members | 2,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,423 IT Pros & Developers. It's quick & easy.

string.Format encoding?

P: n/a
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?
Sep 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com
"Scewbedew" <Sc*******@discussions.microsoft.comwrote in message
news:BF**********************************@microsof t.com...
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?


Sep 28 '06 #2

P: n/a
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com
"Scewbedew" <Sc*******@discussions.microsoft.comwrote in message
news:BF**********************************@microsof t.com...
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?


Sep 28 '06 #3

P: n/a
On Thu, 28 Sep 2006 08:21:02 -0700, Scewbedew
<Sc*******@discussions.microsoft.comwrote:
>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
Sep 28 '06 #4

P: n/a

Scewbedew wrote:
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.

Sep 28 '06 #5

P: n/a
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" wrote:
On Thu, 28 Sep 2006 08:21:02 -0700, Scewbedew
<Sc*******@discussions.microsoft.comwrote:
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
Sep 28 '06 #6

P: n/a
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" wrote:
>
Scewbedew wrote:
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.

Sep 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.