[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Remove non-printable ASCII characters from a string in C#

[Remove non-printable ASCII characters from a string in C#]

The following TrimNonAscii extension method removes the non-printable ASCII characters from a string.

public static string TrimNonAscii(this string value) { string pattern = "[^ -~]+"; Regex reg_exp = new Regex(pattern); return reg_exp.Replace(value, ""); }

In ASCII, the printable characters lie between space (" ") and "~". The code makes a regular expression that represents all characters that are outside of that range repeated one or more times. It uses the expression to create a Regex object and then uses its Replace method to replace those characters with an empty string. The method then returns the resulting string.

Note that this method removes many useful Unicode characters such as £, Æ, and ♥, in addition to fonts such as Cyrillic and Kanji. It's mostly useful for standard English text.

I don't know of a simple way to remove Unicode characters in bulk. You would probably need to make a table of characters that you do or do not want to include and then either loop through the string looking for them or use a Regex object to remove the ones you don't want.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.