In Excel, the CHAR() function allows us to output characters based on the numeric identifier of that character. In this case for testing, CHAR(160) is a non-breaking space , so the contents of A2 appears to match that of A1 , but in fact the spaces on either side for A2 are non-breaking.
The char type takes 1 byte of memory (8 bits) and allows expressing in the binary notation 2^8=256 values. The char type can contain both positive and negative values. The range of values is from -128 to 127.
chr(0) is NULL character, which is very significant and chr(32) is ' ' . The point of NULL character is to terminate strings for example.
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.
char code 160
would be
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".
value.Replace(Convert.ToChar(160).ToString(),"")
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With