Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect SUB character and remove it from a text file in C#?

Tags:

string

c#

text

I am writing a program to process special text files. Some of these text files end with a SUB character (a substitute character. It may be 0x1A.) How do I detect this character and remove it from the text file using C#?

like image 554
user981848 Avatar asked Aug 17 '12 21:08

user981848


1 Answers

If it's really 0x1A in the binary data, and if you're reading it as an ASCII or UTF-8 file, it should end up as U+001A when read in .NET. So you may be able to write something like:

string text = File.ReadAllText("file.txt");
text = text.Replace("\u001a", "");
File.WriteAllText("file.txt", text);

Note that the "\u001a" part is a string consisting of a single character: \uxxxx is an escape sequence for a single UTF-16 code point with the given Unicode value expressed in hex.

like image 189
Jon Skeet Avatar answered Oct 18 '22 04:10

Jon Skeet