Andy Evans
Andy Evans

Reputation: 7176

Remove specific character from a string based on hex value

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?

Upvotes: 11

Views: 40771

Answers (4)

Willem van Ketwich
Willem van Ketwich

Reputation: 5984

string.Replace does work. Without using RegEx:

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

Upvotes: 7

Uwe Keim
Uwe Keim

Reputation: 40736

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.

Upvotes: 3

KeithS
KeithS

Reputation: 71565

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

This ought to do it.

Upvotes: 13

Jackson Pope
Jackson Pope

Reputation: 14640

Why doesn't string.Replace work?

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

Upvotes: 26

Related Questions