0

Getting this error on the String.Format() method and have no idea why, everything looks good to me?

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

2 Answers 2

8

You need to escape the "{", "}" by doubling them as string.Format() considers each '{' or '}' to be part of a placeholder (like '{0}')

Something like this:

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);
Sign up to request clarification or add additional context in comments.

Comments

1

Your two problems are the lines @font-face { and }". When using Format you have to escape curly braces by using two of them. Just change those to lines to @font-face {{ and }}"

1 Comment

@ZeeTee, after @font-face you are using {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.