2012-08-10 32 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]拿到第一个字母

相关问题