Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 834
Diremage Avatar asked Aug 10 '12 02:08

Diremage


People also ask

How do you capitalize the first letter of a string?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it. Now, we would get the remaining characters of the string using the slice() function.

How do you capitalize a string in Swift?

The uppercased() method converts all lowercase characters in a string into uppercase characters.


2 Answers

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];
like image 51
FierceMonkey Avatar answered Oct 01 '22 22:10

FierceMonkey


use [yourString substringToIndex:1] to get first letter

like image 32
cnu Avatar answered Oct 01 '22 21:10

cnu