Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format: Input string was not in a correct format [closed]

The following code keep giving me error saying Input string was not in a correct format, but I am pretty sure it is right, isn't it?

int id = 112;  String[] newData = { "1", "2", "21", "reidb", "reidb", "reidb", "reidb", "aa",                        "Some description", "11", "2012-02-28", "2012-01-29", "true", "1", "1",                        "true", "note note note", "true", "1", "2011-12-03", "45"};  String data = "{ cmd: \"save magellan deal\", data: { id: {0} , AId: {1}, " +             "CId: {2}, CCId:{3}, LA: \"{4}\", BA: \"{5}\" , " +             "LSA: \"{6}\" , BSA: \"{7}\" , \"path: \"{8}\"," +             "dscp: \"{9}\", SI: \"{10}\", CD: \"{11}\", " +             "period: \"{12}\", IsStatic: {13}, LSD: {14}, LC: {15}, RB: {16},} " +             "Notes: \"{17}\", IsEE: {18}, RBy: {19}, DPDD: \"{20}\", LId: {21} } }";   String cmd = String.Format(data, id.toString(), newData); 

Anyone any ideas?

=== EDIT ===

after fixing the braces, a new error of "Index (zero based) must be greater than or equal to zero and less than the size of the argument list." is given. the newData has 21 and plus the id.toString(), should be exact 22?

like image 964
jamesdeath123 Avatar asked Jan 29 '13 00:01

jamesdeath123


People also ask

What does input string mean?

n a device, such as a keyboard, used to insert data directly into a computerized system. input device. n (Computing) a peripheral device that accepts data and feeds it into a computer.

What does it mean to format a string?

Format(String, Object) Replaces one or more format items in a string with the string representation of a specified object. Format(String, Object[]) Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.


2 Answers

Escape the "{", "}" (by duplicating them) in the format string:

"{{ cmd: \"save magellan deal\", data: {{ id: {0} , AId: {1}, " + "CId: {2}, CCId:{3}, LA: \"{4}\", BA: \"{5}\" , " + "LSA: \"{6}\" , BSA: \"{7}\" , \"path: \"{8}\"," + "dscp: \"{9}\", SI: \"{10}\", CD: \"{11}\", " + "period: \"{12}\", IsStatic: {13}, LSD: {14}, LC: {15}, RB: {16},}} " + "Notes: \"{17}\", IsEE: {18}, RBy: {19}, DPDD: \"{20}\", LId: {21} }} }}" 

And you have 22 elements in the format string and 21 in the array.

like image 177
Cédric Bignon Avatar answered Oct 08 '22 01:10

Cédric Bignon


You have 21 elements in newData but use 22 placeholders (numbered 0 to 21).

Also, you must escape literal '{' in your input data

Opening and closing braces are interpreted as starting and ending a format item. Consequently, you must use an escape sequence to display a literal opening brace or closing brace. Specify two opening braces ("{{") in the fixed text to display one opening brace ("{"), or two closing braces ("}}") to display one closing brace ("}"). Braces in a format item are interpreted sequentially in the order they are encountered. Interpreting nested braces is not supported.

http://msdn.microsoft.com/en-us/library/txafckwd.aspx

like image 42
Eric J. Avatar answered Oct 07 '22 23:10

Eric J.