Diremage
Diremage

Reputation: 200

Returning first letter of NSString capitalized

I would like to return the first letter of an NSString capitalized. I have an UISearchDisplayController that displays section titles according to the title of the search results.

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

    return sectionTitle;

}

And to return the letter, in my search function,

[searchSectionTitles addObject:[lastName firstLetter]];

How can I make

- (NSString *)firstLetter

return the first letter of an NSString?

Upvotes: 13

Views: 12503

Answers (2)

SwiftWarrior
SwiftWarrior

Reputation: 1964

the code below will capitalize the first letter of a string, in this case the string to capitalize the first letter of is called sectionTitle

NSString *firstLetter = [[sectionTitle substringToIndex:1];

firstLetter = [firstLetter uppercaseString];

Upvotes: 40

cnu
cnu

Reputation: 815

use [yourString substringToIndex:1] to get first letter

Upvotes: 2

Related Questions