tags:

views:

568

answers:

3

Hi,

I have a C# routine that imports data from a csv file, matches it against a database and then rewrites it to a file. The source file seems to have a few non-ascii characters that are fouling up the processing routine.

I already have a static method that I run each input field through but it performs basic checks like removing commas and quotes. Does anybody know how I could add functionality that removes non-ascii characters too?

Thanks, Chris

+2  A: 

string sOut = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(s))

EToreo
+1  A: 

It sounds kind of strange that it's accepted to drop the non-ASCII.

Also I always recommend the excellent FileHelpers library for parsing CSV-files.

Jonas Elfström
+1 For the FileHelpers link! Looks very useful.
amelvin
A: 

If you wanted to test a specific character, you could use

if ((int)myChar <= 127)

Just getting the ASCII encoding of the string will not tell you that a specific character was non-ASCII to begin with (if you care about that). See MSDN.

Eric J.