Non Printable Characters using C#

G

Greyhound

I need to remove non-printable characters from a text file. I need to do
this in C#. The Hex codes for the characters I need to remove are '0C' and
'0A' which equate to 12 and 10 in decimal. Their codes are 'FF' and 'LF'
from the ASCII talbe. I have searched and searched and cannot come up with a
method for doing this.

Many Thanks...
 
J

Jeroen Mostert

Greyhound said:
I need to remove non-printable characters from a text file. I need to do
this in C#. The Hex codes for the characters I need to remove are '0C' and
'0A' which equate to 12 and 10 in decimal. Their codes are 'FF' and 'LF'
from the ASCII talbe. I have searched and searched and cannot come up with a
method for doing this.
Use a FileStream to read the existing file and a FileStream for creating the
new file. .Read() from one and .Write() to the other, skipping the unwanted
characters. A BufferedStream may improve performance.

Anything more and I'd be writing the code for you, and that would be bad.
 
J

Jon Skeet [C# MVP]

I need to remove non-printable characters from a text file. I need to do
this in C#. The Hex codes for the characters I need to remove are '0C' and
'0A' which equate to 12 and 10 in decimal. Their codes are 'FF' and 'LF'
from the ASCII talbe. I have searched and searched and cannot come up with a
method for doing this.

Just read the text, then remove the appropriate characters using
something like string.Replace. The escape code for FF is "\f" and for
LF it's "\n".

Jon
 
A

Arne Vajhøj

Greyhound said:
I need to remove non-printable characters from a text file. I need to do
this in C#. The Hex codes for the characters I need to remove are '0C' and
'0A' which equate to 12 and 10 in decimal. Their codes are 'FF' and 'LF'
from the ASCII talbe. I have searched and searched and cannot come up with a
method for doing this.

s = Regex.Replace(s, "[\f\n]", "");

or maybe:

s = Regex.Replace(s, "[\u0000-\u001F]", "");

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top