【问题标题】:string.Format() giving "Input string is not in correct format"string.Format() 给出“输入字符串格式不正确”
【发布时间】:2011-10-20 13:22:31
【问题描述】:

我在这里做错了什么?

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);

我明白了

输入的字符串格式不正确

尝试更改 {0} 时。我在 C# 和 WinForms 中这样做。

格式异常未处理
输入字符串的格式不正确

我得到的疑难解答提示:

确保您的方法参数格式正确。将字符串转换为日期时间时,先解析字符串取出日期,然后再将每个变量放入 DateTime 对象中。

【问题讨论】:

    标签: c# winforms string


    【解决方案1】:

    string.Format() 将每个“{”或“}”视为占位符的一部分(例如您已经使用的“{0}”)。您需要通过加倍来逃避每个文字出现。

    所以在你的情况下:

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

    【讨论】:

    • 一个完美的 StackOverflow 问答示例,如果我见过的话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多