Is there an easy way to programmatically get the alphabet?

本秂侑毒 提交于 2019-12-04 08:32:59

问题


I want an NSArray/NSMutableArray containing 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;

回答1:


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]];
}



回答2:


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];



回答3:


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:@" "];



回答4:


try with following code;


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



回答5:


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. ;)



来源:https://stackoverflow.com/questions/4377956/is-there-an-easy-way-to-programmatically-get-the-alphabet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!