4

How can I get a list of strings from "A:" to "Z:" in C#? Something like this:

List<string> list = new List<string>();
for (int i = 0; i < 26; i++)
{
   list.Add(string.Format("{0}:", Convert.ToChar('A' + i));
}

Sorry I don't have VS available for verification right now to verify. By the way, is there web site available to interactive test snip of codes?

1
  • Thanks a lot for all the nice answers. I really like VS 2008 codes, however, I have to use one for VS 2005. Jul 19, 2009 at 20:38

8 Answers 8

14
from ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" select ch + ":";
3
  • I really like the look of this one. Jul 19, 2009 at 21:14
  • 4
    Alternatively: "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Select(ch => ch + ":") - same thing, no need for "from" or "in".
    – Jon Skeet
    Jul 19, 2009 at 21:17
  • 3
    That is a nice one; works also for ranges that do not come in sequence, such as the Swedish alphabet. Jul 19, 2009 at 21:22
12

Using LINQ:

List<string> aToZ = Enumerable.Range('A', 26)
                              .Select(x => (char) x + ":")
                              .ToList();

Not using LINQ - a simpler alternative (IMO) to the original for loop:

List<string> list = new List<string>();
for (char c = 'A'; c <= 'Z'; c++)
{
   list.Add(c + ":");
}
2
  • 2
    Heh, you used lowercase letters though! (just kidding, we have almost identical answers) Jul 19, 2009 at 20:31
  • 7
    You English guys are lucky to have an alphabet that comes in sequence ;o) Jul 19, 2009 at 20:34
4

Well, not counting missing ')' at the end of list.Ad.... line, everything is ok, altough you could write it using a bit shorter notation

list.Add((char)('A' + i) + ":");
1
  • 1
    That's a lot of unnecessary casting etc though - see my revised solution if you're not keen on LINQ.
    – Jon Skeet
    Jul 19, 2009 at 20:47
2

Edit: Y'all should have marked me down for replying without reading. This doesn't work in VS2005, which is what the OP asked about.

List<string> list = new List<string>(Enumerable.Range((int)'A', 26).Select(value => ((char)value).ToString() + ':'));
3
  • No need to explicitly call ToString you use a string literal on the RHS of the +. Or there's string.Format of course.
    – Jon Skeet
    Jul 19, 2009 at 20:33
  • Since it no longer applies, you can delete it. Jul 19, 2009 at 20:52
  • It doesn't apply for the OP, but it applies to the title of the thread and perhaps other people perusing things. :) Jul 19, 2009 at 21:12
1

How about:

var list = Enumerable.Range('a', 'z' - 'a' + 1).Select(charCode => (char)charCode)).ToList();
0

Yours works fine with the exception of missing a ). I test all my snipits with LinqPad. (I don't know how I ever lived without it).

0

For testing code snippets, I use LinqPad or Snippet Compiler. I prefer LinqPad but both are very nice.

0

Other answer ;-)

List<string> list = new List<string>();
for (int i = 'A'; i <= 'Z'; i++)
{
    list.Add(string.Format("{0}:", Convert.ToChar(i)));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.