1

I am using ASP.NET MVC3, C#, .NET 4.5

I need to remove any "Escape" characters which seem to be appearing in a HTML file.

I was trying:

NewHtml = OldHtml.Replace("0x1B","")

But I think I have something wrong here. Please advise.

SamJolly
  • 6,347
  • 13
  • 59
  • 125
  • I suspected as such ..... What should the string be? – SamJolly Jun 25 '15 at 16:19
  • 1
    The replace is correct but I doubt that your string is right.. Try : `NewHtml = OldHtml.Replace( ((char)0x1b).ToString(), "" );` – TaW Jun 25 '15 at 16:22
  • You are a star, that sorted it. If you could make this an answer then I will mark it as such. Thanks again. P.S Gosh I remember PB :) – SamJolly Jun 25 '15 at 16:27

1 Answers1

3

The replace is correct but I doubt that your string is right..

You need to convert the hex value first to a character and then to a string:

string esc = ((char)0x1b).ToString()

To remove the one character in your question use :

NewHtml = OldHtml.Replace( ((char)0x1b).ToString(), "" );

To remove several characters you can do this:

var chars = new char[] { (char)27, (char)0x1b, '\t', '~' };
string NewHtml  = OldHtml;
foreach (var c in chars )  NewHtml = str.Replace(c.ToString(), string.Empty);

I have combined a few different ways to create characters.

Or you can, and probably should go for a Regular Expression..

Several other solutions can be found here..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111