problem with String.Format function.

hi friends,

i got a problem

when i do the following operation.

String.Format("{CN={1}}","hello");

this is giving error while execution.String not in proper format.

but the following thing works, fine

String.Format("{0}CN={1}(2}","{","hello","}");

Is, their any better way to do the above operation.Like, is their any escape sequence for {.

Any suggestions,

Ranu.



Answer this question

problem with String.Format function.

  • Ananda Ganesh

    Thanks for your  reply.

    its working fine with my application.

    can you explain more on this.

    Thanks and Ragrds

    Ranadheer


  • daydreamsy2k

    String.Format("{CN={1}}","hello");

    failed because of two things - firstly a missing { bracket, and secondly it is using {1} it should use {0} as there wasnt a second string paramater - it would fail on an array out of bounds exception if you like!

    Hope that helps

    Ross



  • Kris Nye

    try this:

    String.Format("{{CN={0}}}", "hello");

    while you're at it, pass in a format provider.

    String.Format(System.Globalization.CultureInfo.CurrentCulture, "{{CN={0}}}", "hello"); // culture specific String
    String.Format(System.Globalization.CultureInfo.InvariantCulture, "{{CN={0}}}", "hello"); // invariant string


    Hope that helps.



  • shitbhar

    Hi, ranadheer

    Here I found two useful links about how to use String.Format method: http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx

    http://blog.stevex.net/index.php/string-formatting-in-csharp/

    Enjoy it!

    Thanks



  • mobigital

    apart from the 1 instead of 0, more important is the double brace. basically as soon as the formatter sees { it assumes that whatever falls after it is format data information. The escape sequence for a brace is {{.

  • problem with String.Format function.