473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

string.Format encoding?

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
6 5175
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: WEIWEIWEI | last post by:
Hi Al I'd like to encode a string submitted from a utf-8 form in a aspx page to big5 Any ideas on how to do that I try sth like public static string unicode_big5(string src) { Encoding big5...
0
by: Benoît | last post by:
Hi, I try to detect the encoding type of a string $myString which comes from a html file. $myString = file_get_contents($myFileName); Firstly : I tried to get the charset in <meta...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
4
by: movieknight | last post by:
Hi, I have an application that stores raw .wav files (and also jpgs/bitmaps) within strings, and I need to sometimes convert these strings to byte arrays, and sometimes go from byte arrays back...
4
by: Christina | last post by:
Hey Guys, Currently, I am using the below code: Dim oReqDoc as XmlDocument Dim requiredBytes As Byte() requiredBytes = System.Text.UTF8Encoding.UTF8.GetBytes(oReqDoc.InnerXml). Here, I am...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
4
by: J Peyret | last post by:
Well, as usual I am confused by unicode encoding errors. I have a string with problematic characters in it which I'd like to put into a postgresql table. That results in a postgresql error so I...
5
by: sniipe | last post by:
Hi, I have a problem with unicode string in Pylons templates(Mako). I will print first char from my string encoded in UTF-8 and urllib.quote(), for example string '£ukasz': ...
3
by: ravan1234 | last post by:
Hello Guys, I am having some problem with Unicode string in KATAKANA format. I am decoding string and then storing in db (SYbase). Also while retrieving data I am encoding and then displaying....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.