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

Incorrect string format??

P: n/a
I'm trying to build a string for eventual output to a file. Input
comes from a number of input boxes but I've only test on one text box
content when I get an exception at runtime. The program segment is:

string work;

work = string.Format("//------- {0} ------\r\n", txtPlanet.Text);
work += string.Format("sphere { \\\\ {0} \r\n",
txtPlanet.Text);

The first string.Format () line is okay but at runtime the last line
generates the following exception:

"Input string was not in a correct format."

It's essentially same as its predecessor except for the inclusion of
the left curly-brace which, apparently, isn't escapable nor does it
appear that it needs to be and yet.....

Any suggestions as to what's wrong with this ocde?

Thanks,
Lilith
Jul 2 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Lilith <li****@dcccd.eduwrote:
I'm trying to build a string for eventual output to a file. Input
comes from a number of input boxes but I've only test on one text box
content when I get an exception at runtime. The program segment is:

string work;

work = string.Format("//------- {0} ------\r\n", txtPlanet.Text);
work += string.Format("sphere { \\\\ {0} \r\n",
txtPlanet.Text);

The first string.Format () line is okay but at runtime the last line
generates the following exception:

"Input string was not in a correct format."

It's essentially same as its predecessor except for the inclusion of
the left curly-brace which, apparently, isn't escapable nor does it
appear that it needs to be and yet.....

Any suggestions as to what's wrong with this ocde?
The left brace *is* escapable - with another left brace. Try this:

work += string.Format("sphere {{ \\\\ {0} \r\n",
txtPlanet.Text);

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #2

P: n/a
On Wed, 2 Jul 2008 22:37:32 +0100, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
>Lilith <li****@dcccd.eduwrote:
>I'm trying to build a string for eventual output to a file. Input
comes from a number of input boxes but I've only test on one text box
content when I get an exception at runtime. The program segment is:

string work;

work = string.Format("//------- {0} ------\r\n", txtPlanet.Text);
work += string.Format("sphere { \\\\ {0} \r\n",
txtPlanet.Text);

The first string.Format () line is okay but at runtime the last line
generates the following exception:

"Input string was not in a correct format."

It's essentially same as its predecessor except for the inclusion of
the left curly-brace which, apparently, isn't escapable nor does it
appear that it needs to be and yet.....

Any suggestions as to what's wrong with this ocde?

The left brace *is* escapable - with another left brace. Try this:

work += string.Format("sphere {{ \\\\ {0} \r\n",
txtPlanet.Text);
Ach! I should have thought of that. I was trying the standard whack.
I did a work around with

work += string.Format("sphere {0} \\\\ {1} \r\n",
'{', txtPlanet.Text);

which worked but feels like a hack.

--
Thanks.
Lilith
Jul 2 '08 #3

P: n/a
The left brace *is* escapable - with another left brace. Try this:

Pardon my ignorance, but is there any particular reason why the "{"
canít be escaped the "natural" way? Such as "\{"?
Jul 2 '08 #4

P: n/a
qg**********@mailinator.com wrote:
>The left brace *is* escapable - with another left brace. Try this:

Pardon my ignorance, but is there any particular reason why the "{"
canít be escaped the "natural" way? Such as "\{"?
There is natural and there is natural.

I believe there are as many languages that uses doubling
of the character that needs to be escaped as there are
using \.

C# uses \, but this is a .NET issue not a C# issue. The VB.NET
guys will not consider \ natural (unless they also do C#).

Arne
Jul 2 '08 #5

P: n/a
<qg**********@mailinator.comwrote:
The left brace *is* escapable - with another left brace. Try this:

Pardon my ignorance, but is there any particular reason why the "{"
can=3Ft be escaped the "natural" way? Such as "\{"?
Yup - the backslash is the C# way of escaping a character within a
string. It affects the characters within a string. Here, the string
itself needs to contain both backslashes - because it's being
interpreted by the formatting engine.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #6

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.comwrote:
<qg**********@mailinator.comwrote:
The left brace *is* escapable - with another left brace. Try this:
Pardon my ignorance, but is there any particular reason why the "{"
can=3Ft be escaped the "natural" way? Such as "\{"?

Yup - the backslash is the C# way of escaping a character within a
string. It affects the characters within a string. Here, the string
itself needs to contain both backslashes - because it's being
interpreted by the formatting engine.
Sorry - both braces, not backslashes. As Arne says, it's .NET
interpreting the string, not C# itself.

Having said that, using backslash would have been consistent with its
use in regular expressions...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.