viky
viky

Reputation: 17689

what is wrong in this string?

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

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

Upvotes: 2

Views: 193

Answers (5)

Sebastian Dietz
Sebastian Dietz

Reputation: 5706

I think it should be:

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

Upvotes: 0

Benny
Benny

Reputation: 8815

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

Upvotes: 1

Jørn Schou-Rode
Jørn Schou-Rode

Reputation: 38406

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)

Upvotes: 3

LBushkin
LBushkin

Reputation: 131806

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

Upvotes: 12

Rob Fonseca-Ensor
Rob Fonseca-Ensor

Reputation: 15621

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

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

Upvotes: 2

Related Questions