Remove specific character from a string based on hex value

36,420

Solution 1

Why doesn't string.Replace work?

stringVar.Replace((char)0xA0, ' ');

Solution 2

Regex.Replace(input, "\xA0", String.Empty);

This ought to do it.

Solution 3

string.Replace does work. Without using RegEx:

stringVar = stringVar.Replace("\xA0", string.Empty);

Solution 4

Would this work for you?

var myNewString = myCurrentString.Replace("\n", string.Empty );
myNewString = myNewString.Replace("\r", string.Empty );

"\n" is ASCII LineFeed, "\r" is Return.

Share:
36,420
Andy Evans
Author by

Andy Evans

old school developer trying to learn the new school.

Updated on February 19, 2020

Comments

  • Andy Evans
    Andy Evans about 4 years

    While importing data from a flat file, I noticed that some of lines have embedded non breaking spaces (Hex: A0).

    I would like to remove these, but the standard string.replace doesn't seem to work and had considered using regex to replace the string but wouldn't know what the regex would search for to remove it.

    Rather than converting the whole string to hex and examining that, is there a better way?

  • KeithS
    KeithS over 13 years
    Neither of these are the non-breaking space character A0 (160).
  • Uwe Keim
    Uwe Keim over 13 years
    Yes, you are right, I misunderstood the question. Sorry. Should I delete my answer or just leave it here (I'm new to this...)?
  • Uwe Keim
    Uwe Keim over 13 years
    The answer is completely wrong, nothing to correct here :-) The other guys answered correctly.
  • NealWalters
    NealWalters about 12 years
    The above changes to blank, right? How do you completely remove it? I tried "" and '', but apparently if you have a char on the left of the replace, you have to have a char on the right side.
  • Jackson Pope
    Jackson Pope almost 12 years
    @NealWaters: You need to use the string version of Replace with null as the second parameter.