2
string.Format("{Find Name='{0}'}", name)

it throws Exception at runtime saying input string was in wrong format. What is wrong in this string?

Svetlozar Angelov
  • 21,214
  • 6
  • 62
  • 67
viky
  • 17,275
  • 13
  • 71
  • 90

5 Answers5

12

You need to escape the '{ characters in String.Format:

string.Format( "{{Find Name='{0}'}}", name )

See the following for more details:

How to escape braces (curly brackets) in a format string in .NET

Community
  • 1
  • 1
LBushkin
  • 129,300
  • 32
  • 216
  • 265
3

Curly braces have a special meaning in formatting strings, and thus need to be escaped. Simply double the literal braces from { to {{ and } to }}:

string.Format("{{Find Name='{0}'}}", name)
Jørn Schou-Rode
  • 37,718
  • 15
  • 88
  • 122
2

try string.Format("Find Name='{0}'", name)

or try string.Format("{{Find Name='{0}'}}", name)

Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57
1

it should be "{{ Find Name = {0} }}"

Benny
  • 8,547
  • 9
  • 60
  • 93
0

I think it should be:

string.Format("Find Name='{0}'", name);
Sebastian Dietz
  • 5,587
  • 1
  • 31
  • 39