2012-08-10 69 views
11

我想返回大寫的NSString的第一個字母。我有一個UISearchDisplayController,根據搜索結果的標題顯示部分標題。返回大寫的NSString的第一個字母

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    NSString *sectionTitle; 
    if (searching) 
     sectionTitle = [searchSectionTitles objectAtIndex:section]; 
    else 
     sectionTitle = [[collation sectionTitles] objectAtIndex:section]; 

    return sectionTitle; 

} 

並返回的信,在我的搜索功能,

[searchSectionTitles addObject:[lastName firstLetter]]; 

我怎樣才能讓

- (NSString *)firstLetter 

返回NSString的第一個字母?

回答

40

下面的代碼將利用字符串的第一個字母,在這種情況下,字符串能夠利用被稱爲sectionTitle

NSString *firstLetter = [[sectionTitle substringToIndex:1] 

firstLetter = [firstLetter uppercaseString]; 
的第一個字母
2

使用[yourString substringToIndex:1]拿到第一個字母

相關問題