Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all non-ASCII characters from string

Tags:

c#

ascii

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?

like image 850
user135498 Avatar asked Oct 05 '09 23:10

user135498


People also ask

How do I remove non-ASCII characters from a string?

Remove Non-ASCII Characters From Text Python Here we can use the replace() method for removing the non-ASCII characters from the string. In Python the str. replace() is an inbuilt function and this method will help the user to replace old characters with a new or empty string.

How do I remove non-ASCII characters from a string in Python?

In python, to remove non-ASCII characters in python, we need to use string. encode() with encoding as ASCII and error as ignore, to returns a string without ASCII character use string. decode().

What are non-ASCII characters Python?

In order to use non-ASCII characters, Python requires explicit encoding and decoding of strings into Unicode. In IBM® SPSS® Modeler, Python scripts are assumed to be encoded in UTF-8, which is a standard Unicode encoding that supports non-ASCII characters.


2 Answers

Here a simple solution:

public static bool IsASCII(this string value) {     // ASCII encoding replaces non-ascii with question marks, so we use UTF8 to see if multi-byte sequences are there     return Encoding.UTF8.GetByteCount(value) == value.Length; } 

source: http://snipplr.com/view/35806/

like image 190
Jaider Avatar answered Oct 02 '22 13:10

Jaider


string sOut = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(s)) 
like image 25
EToreo Avatar answered Oct 02 '22 12:10

EToreo