Bayrem Bayrem - 1 month ago 9
C# Question

How to print .NET String.Format space character on a regular string?

I am working on strings in .Net Core. I have a string formatted using

and when it is formatted the output is
123 456,00
. I wanted to assert that the formatted string is equal to the string i wish it to be but i get an
Assert.Equal Failure()
and the problem is in the space character. In the output it asserts that the two spaces are different.

Here is my code :

public void Separator()
var str = string.Format("{0:n}", 123456);
Assert.Equal("123 456,00",str);

I also compared the space character from the formatted string to a regular space character with an assert as follows
Assert.Equal(' ',str[3]);
i get that the expected value is

Why is this happening and how can i get the same character without using

Answer Source

The culture you are using specifies that the number group separator is a different ASCII character than space. I'm guessing you are using ru-RU here, which means the digit is ASCII character 160, which means those strings will not match if you have just typed a space.

You could replace the space with the culture's separator like this for example:

var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
var stringToCompare = "123 456,00".Replace(
    " ", 

Assert.Equal(stringToCompare, str);