string.Format()给出“输入字符串的格式不正确” [英] string.Format() giving "Input string is not in correct format"

查看:62
本文介绍了string.Format()给出“输入字符串的格式不正确”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我该怎么办?

string tmp = @"
    if (UseImageFiles) {
        vCalHeader += ""<td><img onmousedown='' src= '{0}cal_fastreverse.gif' width='13px' height='9' onmouseover='changeBorder(this, 0)' onmouseout='changeBorder(this, 1)' style='border:1px solid white'></td>\n""; //Year scroller (decrease 1 year)
        calHeight += 22;
    }";

string x = "xter";
tmp = string.Format(tmp, x);

我得到


输入字符串的格式不正确

Input string was not in correct format

当尝试更改 {0} 。我正在C#和WinForms中执行此操作。

when trying to change {0}. I am doing this in C# and WinForms.


未处理格式异常

输入字符串的格式不正确

Format Exception was unhandled
Input string was not in correct format

我得到的疑难解答提示:

Troubleshoot tips I get:


制作确保您的方法参数格式正确。将字符串转换为datetime时,在将每个变量放入DateTime对象之前,先分析字符串以取出日期。

Make sure your method arguments are in right format. When converting a string to datetime, parse the string to take out the date before putting each variable into the DateTime object.


推荐答案

string.Format()认为每个 {或}都是占位符的一部分(例如您已经使用的 {0} )。

string.Format() considers each '{' or '}' to be part of a placeholder (like '{0}' you already use). You need to escape each literal occurrence by doubling it.

因此,在您的情况下,请执行以下操作:

So in your case do:

 string tmp = @"
    if (UseImageFiles) {{
        ...
    }}";

这篇关于string.Format()给出“输入字符串的格式不正确”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆