1

Possible Duplicate:
Generating an array of letters in the alphabet in C#

(Theoretical question only, was just pondering it as a writing a filtering system (not using an alphabet, but got me thinking)).

So lets say I want to create a filter list of all the capital letters (A-Z) in the English alphabet plus the word "All"

All A B C D E ... X Y Z 

And convert it to a List<string> what is the most efficient way to do this in C# Without using the hard coded {"A","B"} method.

Not a duplicate of This question

The question listed above deals with conversion to a plain and simple character array, which wouldn't allow for the ALL portion. And to take that and convert I believe would involve at least a copy + cast.

Community
  • 1
  • 1
John Mitchell
  • 9,653
  • 9
  • 57
  • 91
  • @NikolaAnusev not a duplicate I'm afraid, the other one uses a CharArray which wouldn't support the All section, and using this method would require a copy + append. – John Mitchell Jul 29 '12 at 20:47
  • 1
    You are probably right - it's not an *exact* duplicate, but I don't think answers here will be much different. – Nikola Anusev Jul 29 '12 at 20:57
  • 1
    Why do you want to create a list of strings for a list of chars? – Tim Schmelter Jul 29 '12 at 20:59
  • 1
    @TimSchmelter because of the first requirement (adding the word "All" to the list) – John Mitchell Jul 29 '12 at 22:50
  • It's worth mentioning that hard coding it would be the most efficient. If you can't about efficiency to that, if you don't, then so whatever you feel like regardless of efficiency. – Servy Jul 29 '12 at 22:52

5 Answers5

2

You can also do it with actual characters:

List<string> characters = new List<string>();

for (char c = 'A'; c <= 'Z'; c++)
    characters.Add("" + c);
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
  • Or change the first line to `var characters = new List(27) { "All", };`. Then the capacity of the `List<>` will not need to be expanded during the loop. – Jeppe Stig Nielsen Jul 29 '12 at 21:32
2

For 'most efficient' you would try to avoid List<> and LINQ.

  var sb = new StringBuilder("All", 26+3 +spare);
  for (char c = 'A'; c <= 'Z'; c++)  sb.Append(c);

  string result = sb.ToString();

but to be honest you would have to benchmark the various answers here.

H H
  • 263,252
  • 30
  • 330
  • 514
1

Each string character is a char value that has a number an ascii. Capital A starts at 65 and Captial Z is 90. Thus using a loop you can generate the values.

List<string> alpha = new List<string>();
for(int i=65; i <=90; i++) {
   alpha.add(""+(char)i);
}

EDIT:

You could also use the character literals for the for loop as

for(int i = (int)'A'; i <= (int)'Z'; i++)
secretformula
  • 6,414
  • 3
  • 33
  • 56
1

For example:

var alphabet = new List<String>(27);
var capitalRange = Enumerable.Range(0, 26)
    .Select(i => new String(Convert.ToChar(i + 65), 1));
alphabet.AddRange( capitalRange );
alphabet.Add("All");

Note that the initialization of the list with the correct capacity ensures that it doesn't need to be resized and won't be oversized. Apart from that this is similar to a for-loop. The string constuctor is slightly faster than a Char.ToString().

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Here is a fairly compact way to do it:

var list = new[] { "All" }.Concat(
    Enumerable.Range('A', 'Z' - 'A' + 1)
    .Select(c => ((char)c).ToString())
).ToList();

But something like this is cleaner (IMO) and more efficient since there is no resizing:

const char start_ch = 'A';
const char end_ch = 'Z';

var list = new List<string>(end_ch - start_ch + 1) { "All" };

for (char ch = start_ch; ch <= end_ch; ++ch)
    list.Add(ch.ToString());

What exactly do you intend to do with this List? For example, searching can be more efficiently done using an associative data structure instead, such as HashSet or Dictionary.

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
  • Nothing :) it was a theoretical question about micro-optomization thats all. If it was just output then I'd probably use a array, if it was searching then it'd be a dictionary. – John Mitchell Jul 29 '12 at 21:53