Alex Rivera | Logout

How do I get a list of all the printable characters in C#?

Asked 2009-05-20T11:22:45.463
23

I'd like to be able to get a char array of all the printable characters in C#, does anybody know how to do this?

edit:

By printable I mean the visible European characters, so yes, umlauts, tildes, accents etc.

c#
Edit
Report

1 Answer

24

Here's a LINQ version of Fredrik's solution. Note that Enumerable.Range yields an IEnumerable<int> so you have to convert to chars first. Cast<char> would have worked in 3.5SP0 I believe, but as of 3.5SP1 you have to do a "proper" conversion:

var chars = Enumerable.Range(0, char.MaxValue+1)
                      .Select(i => (char) i)
                      .Where(c => !char.IsControl(c))
                      .ToArray();

I've created the result as an array as that's what the question asked for - it's not necessarily the best idea though. It depends on the use case.

Note that this also doesn't consider full Unicode characters, only those in the basic multilingual plane. I don't know what it returns for high/low surrogates, but it's worth at least knowing that a single char doesn't really let you represent everything :(

answered 2009-05-20T11:45:07.773

Your Answer