4

I am trying build a string which have two parts of strings it errors out .....Here is my code below

 StringBuilder str = new StringBuilder();          
            str.AppendFormat("{0}",GetAccessor(attr, rootTblName)).AppendLine();                
            str.Append("}").AppendLine();

            return str.ToString();


 private string GetAccessor(DataAttribute attr, string rootTblName)
        {
            return string.Format("get { return {0}.{1}; }", 
                GetRootPvtMember(rootTblName), 
                attr.MdlPart.InternalName
            );
        }

the error : input string format not valid....... It fails i mean error out due to the "{" ,"}" in the GetAccessor return string with out them its working , but i need them like that . so any work around plz.....

leppie
  • 115,091
  • 17
  • 196
  • 297
mandava
  • 336
  • 4
  • 19
  • You should accept one of the answers given below. I can also see you barely accepted answers in your previous questions, please do accept wherever is applicable. – Abdusalam Ben Haj Feb 20 '13 at 10:41
  • i know its not allowing me to do within 11 mins from the question is asked , i tried – mandava Feb 20 '13 at 10:44

2 Answers2

10

Use double braces {{ to escape curly braces in Format.

Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
  • Suppose if the error message is coming from database, how can we escape with double braces. We can't update in the database because it is using in some other scenarios. – Ashok Yaganti Oct 07 '21 at 21:28
6

Escape them with {{ and }}.

Then it will work.

Eg:

string.Format("get {{ return {0}.{1}; }}", ....
leppie
  • 115,091
  • 17
  • 196
  • 297