什么可能导致 String.Format 意外地移动字符?

发布于 2024-07-30 07:50:24 字数 496 浏览 4 评论 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 / 聚合:0 (0.0%

知道什么会导致括号变得像这样混乱吗?

添加到(或者也许解释?)奇怪之处:这个问题不会发生在我的开发机器上,使用Windows XP。该问题确实出现在我们使用 Windows Server 2008 的生产计算机上(使用相同的代码)。

So I have this code:

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

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

For some mysterious reason, the output ends up being the following string:

(Flips: 0 / Aggr: 0 (0.0%

Any idea what would cause the parentheses to get all messed up like this?

To add to (or perhaps explain?) the strangeness: this problem DOESN'T occur on my development machine, using Windows XP. The string appears as expected. The problem does occur on our production machines (using the same code), using Windows Server 2008.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

习惯成性 2024-08-06 07:50:24

我猜测您正在将此消息输出到某种屏幕控件,其 MaxLength 属性设置为与您的消息相同的长度(或者可能是具有 MaxLength 列类型的 DataTable)。 然而,在消息输出之前,整个字符串有时会被括号包围。

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

在某些机器上,不会添加这些额外的括号,因此不会出现问题。 不过,我不知道为什么它会因机器而异。

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.

梦亿 2024-08-06 07:50:24

这个怎么样:

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

How about this:

String flipsMessage = String.Format(
    "Flips: {0} / Aggr: {1} ({2:P})",
    numFlips, numAggrFlips, pctAggrFlips
);
如果没有你 2024-08-06 07:50:24

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

另外,停止并查看变量 FlipsMessage 在调试模式下的计算结果。

I suspect you are misreading something... in your example, not only is one of the parentheses moved to the beginning, but a closing parenetheses has been replaced by an opening parentheses... My best guess based on the minimal data you present is that the output you are showing is being generated by a code snippet somewhere else in your solution that you are not looking at... I Would carefully step through the code, in debug mode, line by line, and make sure that this line is actually the one that is ouputting the displayed value.

Also, stop, and see what the value of the variable flipsMessage evaluates to in debug mode.

二智少女猫性小仙女 2024-08-06 07:50:24

嗯,我猜你在某个地方偷偷地藏着一个回车符。

hmmm I'd guess you have a carriage-return sneaking in somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文