views:

38

answers:

2

I have the following text in a file...

blah blah ñ blah

Notice the ñ symbol.

I'm reading this with StreamReader.ReadLine and then trying a string.Replace to replace the special character.

For some reason this isn't working, and it seems to be something to do with the StreamReader. When I inspect the string in the debugger after reading the line, I get that box shape that text editors display when they can't render a character.

Trying similar directly in code works fine...

int test = "helloñworld".IndexOf('ñ');

so I figure it must be the StreamReader.

Any ideas?

+2  A: 

Create the StreamReader with the correct encoding of your text file using either the StreamReader(Stream, Encoding) constructor or StreamReader(String, Encoding). Common encodings nowadays are Latin 1 or UTF-8.

Joey
How do I know what encoding my text file is in?
fearofawhackplanet
@fear: You saved it, I presume. Depending on your editor that could be anything from the OEM charset (CP850 or 437), Latin 1 to Unicode (UTF-8 or UTF-16). I would guess CP1252 to be the most likely option, though.
Joey
No I didn't save it, it's an export from a 20 year old database. Anyway, with trial and error it seems UTF7 works.
fearofawhackplanet
A: 

Hi, Set the correct encoding in overloaded Streamreader constructor.

Hope this helps, Ilya

portland