2016-08-26 26 views

回答

2

我有爲你編寫一個程序,它將在NSString中打印所有字母。

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

enter image description here

+0

謝謝你幫助我。 –

+0

@ pkc456 +1爲您的優化代碼。 –

2

如果您記錄所有字母A到Z,那麼你可以使用:

for (char c = 'a'; c <= 'z'; c++) { 
    NSString *firstLetter = [NSString stringWithFormat:@"%c", c]; 
NSLog("%@",firstLetter); 
} 
+0

我做到了,謝謝你。 –

+0

@AbhilashGupta歡迎您,如果我的回答對您有幫助,請將其提供給正確的商標。這對其他開發人員和用戶會有幫助。 –

0

但是如果你想打印所有字母的NSString比使用這些:

NSString *string = @"abcd"; 
for (NSUInteger index = 0; index < string.length; index++) { 
    unichar letter = [string characterAtIndex:index]; 
    NSLog(@"%c", letter); 

} 
相關問題