空间在返回的String.Format不匹配源$ C ​​$ C宣告空间.NET字符串 - 多次重新presentations? [英] Space in a .NET string returned by string.Format does not match space declared in source code - multiple representations?

查看:106
本文介绍了空间在返回的String.Format不匹配源$ C ​​$ C宣告空间.NET字符串 - 多次重新presentations?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过的String.Format返回的字符串似乎用一些奇怪的编码。包含在格式字符串中的空格被重新presented相比,包含在源$ C ​​$ C声明串空间使用不同的字节值。

String returned by string.Format seems to use some strange encoding. Spaces contained in format string are represented using different byte values compared to spaces contained in strings declared in source code.

下面的测试案例演示了此问题:

The following test case demonstrates the problem:

[Test]
public void FormatSize_Regression() 
{
  string size1023 = FileHelper.FormatSize(1023);
  Assert.AreEqual("1 023 Bytes", size1023);
}

失败:


    String lengths are both 11. Strings differ at index 1.
    Expected: "1 023 Bytes"
    But was:  "1 023 Bytes"
    ------------^

FORMATSIZE方式:

FormatSize method:

public static string FormatSize(long size) 
{
  if (size < 1024)
     return string.Format("{0:N0} Bytes", size);
  else if (size < 1024 * 1024)
     return string.Format("{0:N2} KB", (double)((double)size / 1024));
  else
     return string.Format("{0:N2} MB", (double)((double)size / (1024 * 1024)));
}

从VS时,断点设置的断言行即时窗口:

From VS immediate window when breakpoint is set on the Assert line:

size1023
"1 023 Bytes"

System.Text.Encoding.UTF8.GetBytes(size1023)
{byte[12]}
    [0]: 49
    [1]: 194 <--------- space is 194/160 here? Unicode bytes indicate that space should be the 160. What is the 194 then?
    [2]: 160
    [3]: 48
    [4]: 50
    [5]: 51
    [6]: 32
    [7]: 66
    [8]: 121
    [9]: 116
    [10]: 101
    [11]: 115
System.Text.Encoding.UTF8.GetBytes("1 023 Bytes")
{byte[11]}
    [0]: 49
    [1]: 32  <--------- space is 32 here
    [2]: 48
    [3]: 50
    [4]: 51
    [5]: 32
    [6]: 66
    [7]: 121
    [8]: 116
    [9]: 101
    [10]: 115

System.Text.Encoding.Unicode.GetBytes(size1023)
{byte[22]}
    [0]: 49
    [1]: 0
    [2]: 160 <----------- 160,0 here
    [3]: 0
    [4]: 48
    [5]: 0
    [6]: 50
    [7]: 0
    [8]: 51
    [9]: 0
    [10]: 32
    [11]: 0
    [12]: 66
    [13]: 0
    [14]: 121
    [15]: 0
    [16]: 116
    [17]: 0
    [18]: 101
    [19]: 0
    [20]: 115
    [21]: 0
System.Text.Encoding.Unicode.GetBytes("1 023 Bytes")
{byte[22]}
    [0]: 49
    [1]: 0
    [2]: 32 <----------- 32,0 here
    [3]: 0
    [4]: 48
    [5]: 0
    [6]: 50
    [7]: 0
    [8]: 51
    [9]: 0
    [10]: 32
    [11]: 0
    [12]: 66
    [13]: 0
    [14]: 121
    [15]: 0
    [16]: 116
    [17]: 0
    [18]: 101
    [19]: 0
    [20]: 115
    [21]: 0

问:如何这可能

推荐答案

我怀疑你现在的文化是用一种有趣的千分隔符 - U + 00A0,这是不换行空格字符。这不是一个完全不合理的千位分隔符,说实话......这意味着你不应该得到的文字是这样显示的:

I suspect your current culture is using an interesting "thousands" separator - U+00A0, which is the non-breaking space character. That's not an entirely unreasonable thousands separator, to be honest... it means you shouldn't get text like this displayed:

The size of the file is 1
023 bytes.

相反,你会得到

Instead you'd get

The size of the file is
1 023 bytes.

在我的盒子,我得到1023代替。你想你的 FORMATSIZE 的方法来使用当前区域性或特定的一个?如果它是当前的文化,你应该让你的单元测试中指定的文化。我有一对夫妇的包装方法我用了这一点:

On my box, I get "1,023" instead. Do you want your FormatSize method to use the current culture, or a specific one? If it's the current culture, you should probably make your unit test specify the culture. I have a couple of wrapper methods I use for this:

internal static void WithInvariantCulture(Action action)
{
    WithCulture(CultureInfo.InvariantCulture, action);
}

internal static void WithCulture(CultureInfo culture, Action action)
{
    CultureInfo original = Thread.CurrentThread.CurrentCulture;
    try
    {
        Thread.CurrentThread.CurrentCulture = culture;
        action();
    }
    finally
    {
        Thread.CurrentThread.CurrentCulture = original;
    }            
}

这样我就可以运行:

so I can run:

WithInvariantCulture(() =>
{
    // Body of test
};

等。

如果你想测试你得到确切的字符串,可以使用:

If you want to test for the exact string you're getting, you can use:

Assert.AreEqual("1\u00A0023 Bytes", size1023);

这篇关于空间在返回的String.Format不匹配源$ C ​​$ C宣告空间.NET字符串 - 多次重新presentations?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆