Remove all non-ASCII characters from string

后端 未结 7 1162
野的像风
野的像风 2020-12-05 06:28

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

7条回答
  •  甜味超标
    2020-12-05 06:46

    Here's an improvement upon the accepted answer:

    string fallbackStr = "";
    
    Encoding enc = Encoding.GetEncoding(Encoding.ASCII.CodePage,
      new EncoderReplacementFallback(fallbackStr),
      new DecoderReplacementFallback(fallbackStr));
    
    string cleanStr = enc.GetString(enc.GetBytes(inputStr));
    

    This method will replace unknown characters with the value of fallbackStr, or if fallbackStr is empty, leave them out entirely. (Note that enc can be defined outside the scope of a function.)

提交回复
热议问题