开发者

Is there an easy way to programmatically get the alphabet?

I want an NSArray/NSMutableArray conta开发者_运维技巧ining all the letters of the alphabet. There must be a quick and easy way, better than typing them all out. For example in PHP:

foreach(range('A','Z') as $i) $alphabet[]=$i;


The array generated for table index titles may also be used. It does not use a for loop and has multi-language support.

NSMutableArray *alphabets = [[NSMutableArray alloc] initWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];

//Remove the last object (extra), '#' from the array.
[alphabets removeLastObject];


There's no quicker way than typing them all out, unless you cut and paste my handy reference from below!

"abcdefghijklmnopqrstuvwxyz"


For the sake of it, here's a longer way.

for (char a = 'a'; a <= 'z'; a++)
{
  [myArray addObject:[NSString stringWithFormat:@"%c", a]];
}


Sometimes typing the letters out is the easiest. Here they are as an array:

NSArray *letters = [@"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" componentsSeparatedByString:@" "];


try with following code;


int a = 65;
for (; a < 91; a++) {
    [array addObject:[NSString stringWithFormat:@"%c", (char)a]];
}
NSLog(@"%@", array);


You could use a for-loop to generate them, but I think typing them out is easier. It is most certainly easier than posting a question here. ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜