Replace method not working for escaped char '\xA0' in C#

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

2 Answers

  1. Bruce- Reply


    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

  2. Carl- Reply


    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]+", "");

    My results:

      Ph0b0x �  


    Regex pattern found here

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>