-2

I'm trying to replace '\xA0' character in a string to be blank or worst case a space. This code does compile and run, but it won't replace nbsp character with a space.

string line = Stream.ReadLine();
line = line.Replace('\xA0', ' ');

Does anyone know an alternative solution or had this issue? enter image description here

Jtuck
  • 300
  • 4
  • 7
  • 1
    Are you sure that character is \xA0? – itsme86 Jun 12 '18 at 21:05
  • [It isn't](https://dotnetfiddle.net/JCk3gk). You should inspect the individual characters of the string to find out what it really is, then work back to find whatever encoding issues you might have. – TypeIA Jun 12 '18 at 21:09
  • 1
    The problem is most likely not there. You need to go back to your `StreamReader` and make sure you initialize it with the correct encoding. – Nyerguds Jun 12 '18 at 22:07
  • How is the problem in the SteamReader? I can read the exact same data in Notepad++ so doesn't make sense.. – Jtuck Jun 13 '18 at 12:22
  • Notepad++ does autodetection of the text encoding. `StreamReader` doesn't. – Nyerguds Feb 19 '19 at 13:23
  • @Nyerguds can you elaborate on your explanation. – Jtuck Feb 20 '19 at 14:18
  • [Here you go](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/). – Nyerguds Feb 20 '19 at 22:17

2 Answers2

1

I found a reasonable solution to this question. So I convert the &nbsp � character into a int (65533) so I use that value and use the ToChar() method to get my character and then replace using that character.

        string line = Stream.ReadLine();
        char SpecNBSPChar = Convert.ToChar(65533);
        line = line.Replace(SpecNBSPChar , ' ');

SpecNBSPChar Replace

After the string replace method. Proof it worked

Jtuck
  • 300
  • 4
  • 7
-1

Based on this thread can you try?

line.Replace('\u00A0', ' ');

OK, i am trying to redeem myself here. What about using a Regex? Like this:

string text = " Ph0b0x � ";
string result = Regex.Replace(text, @"[^\x00-\x7F]+", "");
Console.WriteLine(text);
Console.WriteLine(result);
Console.ReadLine();

My results:

  Ph0b0x �  

 Ph0b0x

Regex pattern found here

Ph0b0x
  • 664
  • 7
  • 20
  • 3
    `'\u00A0'` and `'\xA0'` [are equivalent](https://stackoverflow.com/questions/32175482/what-is-the-difference-between-using-u-and-x-while-representing-character-lite), so this won't help. – TypeIA Jun 12 '18 at 21:20
  • Ph0b0x thanks for effort but I did try that thread before posting this question. Doesn't work :( – Jtuck Jun 13 '18 at 12:20
  • @Jtuck can you please try with the code i just added? – Ph0b0x Jun 13 '18 at 13:27