I got a file with characters like these: à, è, ì, ò, ù - À. What i need to do is replace those characters with normal characters eg: à = a, è = e and so on..... This is my code so far:
StreamWriter sw = new StreamWriter(@"C:/JoinerOutput.csv");
string path = @"C:/Joiner.csv";
string line = File.ReadAllText(path);
if (line.Contains("à"))
{
string asAscii = Encoding.ASCII.GetString(Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding(Encoding.ASCII.EncodingName, new EncoderReplacementFallback("a"), new DecoderExceptionFallback()), Encoding.UTF8.GetBytes(line)));
Console.WriteLine(asAscii);
Console.ReadLine();
sw.WriteLine(asAscii);
sw.Flush();
}
Basically this searches the file for a specific character and replaces it with another. The problem that i am having is that my if statement doesn't work. How do i go about solving this?
This is a sample of the input file:
Dimàkàtso Mokgàlo Màmà Ràtlàdi Koos Nèl Pàsèkà Modisè Jèrèmiàh Morèmi Khèthiwè Buthèlèzi Tiànà Pillày Viviàn Màswàngànyè Thirèshàn Rèddy Wàdè Cornèlius ènos Nètshimbupfè
This is the output if use : line = line.Replace('à', 'a'); :
Ch�rl�n� Kirst�n M�m� R�tl�di Koos N�l P�s�k� Modis� J�r�mi�h Mor�mi Kh�thiw� Buth�l�zi Ti�n� Pill�y Vivi�n M�sw�ng�ny� Thir�sh�n R�ddy W�d� Corn�lius �nos N�tshimbupf�
With my code the symbol will be removed completely
Others have commented on using a Unicode lookup table to remove Diacritics. I did a quick Google search and found this example. Code shamelessly copied, (re-formatted), and posted below:
So, your code could clean the input by calling:
Why are you making things complicated?
Update:
The docs for
File.ReadAllText
say:What encoding is
C:/Joiner.csv
in? Maybe you should use the other overload forFile.ReadAllText
where you specify the input encoding yourself?Use this:
Don't know if it is useful but in an internal tool to write message on a led screen we have the following replacements (i'm sure that there are more intelligent ways to make this work for the unicode tables, but this one is enough for this small internal tool) :
One thing to note is that if in most language the text is still understandable after such a treatment it's not always the case and will often force the reader to refer to the context of the sentence to be able to understand it. Not something you want if you have the choice.
Note that the correct solution would be to use the unicode tables, replacing characters with integrated diacritics with their "combined diacritical mark(s)"+character form and then removing the diacritics...
Doing it the easy way. The code below will replace all special characters to ASCII characters in just 2 lines of code. It gives you the same result as Julien Roncaglia's solution.
Sounds like what you want to do is convert Extended ASCII (eight-bit) to ASCII (seven-bit) - so searching for that might help.
I've seen libraries to handle this in other languages but have never had to do it in C#, this looks like it might be somewhat enlightening though:
Convert two ascii characters to their 'corresponding' one character extended ascii representation