0

所以我有这个代码:

/* variables already initialized:
   int numFlips
   int numAggrFlips
   double pctAggrFlips
*/

String flipsMessage = String.Format(
    "Flips: {0} / Aggr: {1} ({2})",
    numFlips, numAggrFlips, pctAggrFlips.ToString("0.0%")
);

由于某种神秘的原因,输出最终成为以下字符串:

(翻转:0 / Aggr:0(0.0%

知道什么会导致括号像这样乱七八糟吗?

增加(或解释?)奇怪之处:这个问题不会出现在我的开发机器上,使用 Windows XP。字符串按预期显示。这个问题确实发生在我们的生产机器上(使用相同的代码),使用 Windows Server 2008。

4

4 回答 4

3

这个怎么样:

String flipsMessage = String.Format(
    "Flips: {0} / Aggr: {1} ({2:P})",
    numFlips, numAggrFlips, pctAggrFlips
);
于 2009-07-28T19:23:13.590 回答
2

我怀疑您误读了某些内容...在您的示例中,不仅一个括号移到了开头,而且右括号已被左括号代替...根据您提供的最小数据,我的最佳猜测是您正在显示的输出是由您未查看的解决方案中其他位置的代码片段生成的......我会在调试模式下逐行仔细地逐步执行代码,并确保这一行是实际上是输出显示值的那个。

另外,停下来,看看在调试模式下变量 flipsMessage 的值是什么。

于 2009-07-28T19:31:57.613 回答
1

I'm going to guess that you are outputting this message to some sort of screen control whose MaxLength property is set to the same length as your message (or perhaps a DataTable with a column type that has a MaxLength). Before the message gets output, however, the entire string is sometimes getting surrounded by parentheses.

flipsMessage = Flips: 0 / Aggr: 0 (0.0%) // length: 25
flipsWParens = (Flips: 0 / Aggr: 0 (0.0%)) // length: 27
flipsTrunced = (Flips: 0 / Aggr: 0 (0.0% // length: 25

On some machines, those extra parentheses don't get added, so you don't have the problem. I have no idea why it would be different from machine to machine, though.

于 2009-07-28T19:55:22.213 回答
0

嗯,我猜你有回车偷偷溜进某个地方。

于 2009-07-28T19:21:21.190 回答