tags:

views:

612

answers:

4

I am formatting numbers to string using the following format string "# #.##", at some point I need to turn back these number strings like (1 234 567) into something like 1234567. I am trying to strip out the empty chars but found that

value = value.Replace(" ", "");  

for some reason and the string remain 1 234 567. After looking at the string I found that

value[1] is 160.

I was wondering what the value 160 means?

+8  A: 

The answer is to look in Unicode Code Charts - where you'll find the Latin-1 supplement chart; this shows that U+00A0 (160 as per your title, not 167 as per the body) is a non-breaking space.

Jon Skeet
+5  A: 

char code 160 would be  

S.Mark
+7  A: 

Maybe you could to use a regex to replace those empty chars:

Regex.Replace(input, @"\p{Z}", "");

This will remove "any kind of whitespace or invisible separator".

Rubens Farias
+2  A: 

This is a fast (and fairly readable) way of removing any characters classified as white space using Char.IsWhiteSpace:

StringBuilder sb = new StringBuilder (value.Length);
foreach (char c in value)
{
    if (!char.IsWhiteSpace (c))
        sb.Append (c);
}
string value= sb.ToString();

As dbemerlin points out, if you know you will only need numbers from your data, you would be better use Char.IsNumber or the even more restrictive Char.IsDigit:

StringBuilder sb = new StringBuilder (value.Length);
foreach (char c in value)
{
    if (char.IsNumber(c))
        sb.Append (c);
}
string value= sb.ToString();

If you need numbers and decimal seperators, something like this should suffice:

StringBuilder sb = new StringBuilder (value.Length);
foreach (char c in value)
{
    if (char.IsNumber(c)|c == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator )
        sb.Append (c);
}
string value= sb.ToString();
Colin Pickard
Thanks for the code sample. I will reuse this in my code.
gyurisc
in this case Char.IsNumber() would be more fitting imho
dbemerlin
@dbemerlin, you are right, I have updated my answer.
Colin Pickard
How about the decimal separator? Will Char.IsDigit() and Char.IsNumber() skip that too?
gyurisc
@AskAboutGadgets.com, yes IsNumber and IsDigit return false for a decimal seperator. I have added a new example
Colin Pickard