Get an array of all alpha characters
Possible duplicate:
List of char from list "A" to list "List .
Is there an easy way to get char[]
all alpha characters?
I know I can do something like this:
char[] alphas = new char[]{'a', 'b', 'c', 'd', ..............};
For all upper and lower case, but I'm wondering if there is a way (and cleaner) to do this.
+3
source to share
5 answers
Enumerable.Range((Int32)'A', 2 * 26).Select(c => (Char)c).ToArray();
Uppps, doesn't work - there are some non-letters between Z and a.
Enumerable.Range((Int32)'A', 26)
.SelectMany(c => new [] { (Char)c, (Char)(c + 'a' - 'A' })
.ToArray();
This solves the problem of the first try, but it's not very clean. Also note that this approach will alternate between uppercase and lowercase letters. I am probably sticking with the following solution.
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToArray()
Or even better, try to avoid this array in the first place. May Char.IsLetter()
be helpful. Or regular expressions.
+6
source to share
char[] upperChars = Enumerable.Range(65, 26).Select(c => (Char)c).ToArray();
char[] lowerChars = Enumerable.Range(97, 26).Select(c => (Char)c).ToArray();
char[] allChars =
(Enumerable.Range(65, 26).Select(c => (Char) c)
.Union(Enumerable.Range(97, 26).Select(c => (Char) c)))
.ToArray();
+4
source to share