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?
在修复了括号之后,“索引(从零开始)的一个新错误必须大于或等于0,小于参数列表的大小。”newData有21个,加上id.toString(),应该是22吗?
5 个解决方案
#1
67
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.
在格式字符串中有22个元素在数组中有21个元素。
#2
5
You have 21 elements in newData but use 22 placeholders (numbered 0 to 21).
在newData中有21个元素,但是使用22个占位符(编号为0到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
http://msdn.microsoft.com/en-us/library/txafckwd.aspx
#3
2
In addition to escaping your curly braces also try changing your String[]
definition to:
除了转义大括号外,还可以尝试将字符串[]定义改为:
int id = 112;
String[] newData = {id.ToString(), "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"};
and change your String.Format
to:
和改变你的字符串。格式:
String cmd = String.Format(data,newData);
#4
2
I've said as much in the comments, but I think it's worth an answer by now:
我在评论中已经说过很多了,但我认为现在值得一个答案:
Don't use string.Format
(in this case). It's slower, and less readable. Just use plain string concatenation with meaningful variable names if you need complex concatenations - these will be faster and more maintainable. Faster because for a fixed number of segments plain +
is the fastest option due to compiler optimizations, and more maintainable because the reader can see the context within the string in which the variable is put.
不使用字符串。格式(在本例中)。它更慢,可读性也更低。如果需要复杂的连接,只需使用简单的字符串连接,并使用有意义的变量名——这些将会更快、更容易维护。更快,因为对于固定数量的段,plain +是由于编译器优化而获得的最快的选项,而且更容易维护,因为读者可以看到变量被放置在字符串中的上下文。
As Hans Passant noted, it looks like you're making a javascript literal. If that's the case, consider a Json serializer like Json.NET instead:
正如Hans Passant所指出的,看起来你在做一个javascript文字。如果是这样,考虑Json这样的Json序列化器。网:
JsonConvert.SerializeObject(new {
cmd = "save magellan deal",
data = new {
id = 12345, AId = 1, CId = 2, CCId = 21,
LA = "reidb", BA = "reidb", LSA = "reidb", BSA = "reidb",
path = "aa", dscp = "Some description", SI = 11,
CD = "2012-02-28", period = "2012-01-29",
IsStatic = true, LSD = 1, LC = 1, RB = true,
},
Notes = "note note note",
IsEE = true, RBy = 1, DPDD = "2011-12-03", LId = 45
})
While Json.NET
does support DateTime
serialization, there's not standardized format for dates in JS, so these are serialized as strings in an iso 8601 format. This might not be what you need, hence the date strings in this example - I'd try the iso format first, though, as that's the simplest solution.
虽然Json。NET支持DateTime序列化,在JS中没有标准化的日期格式,因此这些都是以iso 8601格式的字符串序列化的。这可能不是您所需要的,因此在本例中是日期字符串—我将首先尝试iso格式,因为这是最简单的解决方案。
#5
1
What
什么
String cmd = String.Format(data, id.toString(), newData);
does is: trying to format the String data , using just 2 strings..not 22!!
Which ones?...1)id.ToString() and 2)newData.ToString()
Thats obviously wrong
做的是:尝试格式化字符串数据,只使用2个字符串。不是22 ! !什么? 1)id.ToString()和2)newData.ToString()显然是错误的?
How you can see that this is the problem...? Leave just {0} and {1} in data string and print it.It compiles ok and see what you get.
Of course you have to use {{ instead of { where you want a string literal
你怎么能看出这是一个问题?在数据字符串中保留{0}和{1},并将其打印出来。它编译好的,然后看看你得到了什么。当然,你必须使用{{而不是{你想要一个字符串文字的地方。
Fully working Solution:
完全工作的解决方案:
int id = 112;
String[] newData = { id.ToString(),"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, newData);
#1
67
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.
在格式字符串中有22个元素在数组中有21个元素。
#2
5
You have 21 elements in newData but use 22 placeholders (numbered 0 to 21).
在newData中有21个元素,但是使用22个占位符(编号为0到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
http://msdn.microsoft.com/en-us/library/txafckwd.aspx
#3
2
In addition to escaping your curly braces also try changing your String[]
definition to:
除了转义大括号外,还可以尝试将字符串[]定义改为:
int id = 112;
String[] newData = {id.ToString(), "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"};
and change your String.Format
to:
和改变你的字符串。格式:
String cmd = String.Format(data,newData);
#4
2
I've said as much in the comments, but I think it's worth an answer by now:
我在评论中已经说过很多了,但我认为现在值得一个答案:
Don't use string.Format
(in this case). It's slower, and less readable. Just use plain string concatenation with meaningful variable names if you need complex concatenations - these will be faster and more maintainable. Faster because for a fixed number of segments plain +
is the fastest option due to compiler optimizations, and more maintainable because the reader can see the context within the string in which the variable is put.
不使用字符串。格式(在本例中)。它更慢,可读性也更低。如果需要复杂的连接,只需使用简单的字符串连接,并使用有意义的变量名——这些将会更快、更容易维护。更快,因为对于固定数量的段,plain +是由于编译器优化而获得的最快的选项,而且更容易维护,因为读者可以看到变量被放置在字符串中的上下文。
As Hans Passant noted, it looks like you're making a javascript literal. If that's the case, consider a Json serializer like Json.NET instead:
正如Hans Passant所指出的,看起来你在做一个javascript文字。如果是这样,考虑Json这样的Json序列化器。网:
JsonConvert.SerializeObject(new {
cmd = "save magellan deal",
data = new {
id = 12345, AId = 1, CId = 2, CCId = 21,
LA = "reidb", BA = "reidb", LSA = "reidb", BSA = "reidb",
path = "aa", dscp = "Some description", SI = 11,
CD = "2012-02-28", period = "2012-01-29",
IsStatic = true, LSD = 1, LC = 1, RB = true,
},
Notes = "note note note",
IsEE = true, RBy = 1, DPDD = "2011-12-03", LId = 45
})
While Json.NET
does support DateTime
serialization, there's not standardized format for dates in JS, so these are serialized as strings in an iso 8601 format. This might not be what you need, hence the date strings in this example - I'd try the iso format first, though, as that's the simplest solution.
虽然Json。NET支持DateTime序列化,在JS中没有标准化的日期格式,因此这些都是以iso 8601格式的字符串序列化的。这可能不是您所需要的,因此在本例中是日期字符串—我将首先尝试iso格式,因为这是最简单的解决方案。
#5
1
What
什么
String cmd = String.Format(data, id.toString(), newData);
does is: trying to format the String data , using just 2 strings..not 22!!
Which ones?...1)id.ToString() and 2)newData.ToString()
Thats obviously wrong
做的是:尝试格式化字符串数据,只使用2个字符串。不是22 ! !什么? 1)id.ToString()和2)newData.ToString()显然是错误的?
How you can see that this is the problem...? Leave just {0} and {1} in data string and print it.It compiles ok and see what you get.
Of course you have to use {{ instead of { where you want a string literal
你怎么能看出这是一个问题?在数据字符串中保留{0}和{1},并将其打印出来。它编译好的,然后看看你得到了什么。当然,你必须使用{{而不是{你想要一个字符串文字的地方。
Fully working Solution:
完全工作的解决方案:
int id = 112;
String[] newData = { id.ToString(),"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, newData);