String.format (Error 'Input string was not in a correct format' ) - because of curly braces

String.Format and Input string was not in a correct format Error

http://blogs.thesitedoctor.co.uk/tim/2006/10/02/StringFormat+And+Input+String+Was+Not+In+A+Correct+Format+Error.aspx

 

Monday, October 02, 2006 6:26:29 PM (GMT Daylight Time, UTC+01:00)

I ran into (another) interesting error today when using String.Format. I was getting the error message "Input string was not in a correct format .". In this instance I was calling the content from an XML file, the data was wrapped in CDATA tags so there shouldn't have been an issue with line breaks etc. On investigating the error further I found it was being caused because the content was a HTML page. More specifically, the String.Format error "Input string was not in a correct format." was being caused by the CSS declaration's curly braces.

To work around the method perceiving them as placeholders, simply replace each curly brace "{" with double braces "{{".

To replicate the error:

 

String .Format("<style type=/"text/css/">p{font-size: 1.2em;}</style><p>Your username is: {0}</p>" , "UserName" );

The fix in place:


String .Format("<style type=/"text/css/">p{{ font-size: 1.2em;}} </style><p>Your username is: {0}</p>" , "UserName" );


1 The String.Format method accepts a format string which can include place holders (designated by the curly braces {} and a number that refers to the location of the item that should appear in the list). A quick example of String.Format:

 

String .Format("This is some text the date is {0}." , DateTime .Today.ToShortDateString);


Would produce: "This is some text the date is 02/10/2006"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值