【问题标题】:String.Format() -- Input string was not in a correct formatString.Format() -- 输入字符串的格式不正确
【发布时间】:2014-09-10 04:59:32
【问题描述】:

String.Format() 方法上出现此错误,不知道为什么,我觉得一切都很好?

string fontface = "";

foreach(...)
{
  fontface = String.Format(@"{3}
    @font-face {
        font-family: '{0}';
        src: url('{0}.eot');
        src: url('{0}.eot') format('embedded-opentype'),
                url('{0}.woff2') format('woff2'),
                url('{0}.woff') format('woff'),
                url('{0}.ttf') format('truetype'),
                url('{0}.svg#{0}') format('svg');
        font-weight:{1};
        font-style:{2};
    }"
  , family, weight, style, fontface);
}

【问题讨论】:

    标签: c# asp.net-mvc razor


    【解决方案1】:

    您需要通过将"{", "}" 加倍来转义它们,因为string.Format() 将每个'{''}' 视为占位符的一部分(如'{0}'

    类似这样的:

    fontface = String.Format(@"{3}
        @font-face {{
            font-family: '{0}';
            src: url('{0}.eot');
            src: url('{0}.eot') format('embedded-opentype'),
                    url('{0}.woff2') format('woff2'),
                    url('{0}.woff') format('woff'),
                    url('{0}.ttf') format('truetype'),
                    url('{0}.svg#{0}') format('svg');
            font-weight:{1};
            font-style:{2};
        }}"
      , family, weight, style, fontface);
    

    【讨论】:

      【解决方案2】:

      您的两个问题是@font-face {}" 行。使用 Format 时,您必须使用其中的两个来转义花括号。只需将这些更改为 @font-face {{}}" 的行

      【讨论】:

      • @ZeeTee,在@font-face 之后你正在使用{
      猜你喜欢
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多