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


Perhaps something like this:



string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char [] alphas = (alphabet + alphabet.ToLower()).ToCharArray();

      

+8


source


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


How "clean" it is:

char[] alphas = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray();

      

How about non-English characters?

+5


source


        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


you are converting the ascii value i to char value. 65 is the ascii value 'A' and 90 from "Z" 97 = "a" 122 = "z"

char [] chars = new char[52]
    for(int i=65;i<=90;i++)
     char[i-65]= ConvertToChar(i);

    for(int i=97,j=26;i<=122;i++,j++)
       char[j]= ConvertToChar(i);

      

+1


source







All Articles