Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize the first word of the sentence in Objective-C?

I've already found how to capitalize all words of the sentence, but not the first word only.

NSString *txt =@"hi my friends!"
[txt capitalizedString];

I don't want to change to lower case and capitalize the first char. I'd like to capitalize the first word only without change the others.

like image 703
wal Avatar asked Mar 12 '10 11:03

wal


People also ask

How do you capitalize the first word in a sentence?

You should always capitalize the first letter of the first word in a sentence, no matter what the word is. Take, for example, the following sentences: The weather was beautiful. It was sunny all day. Even though the and it aren't proper nouns, they're capitalized here because they're the first words in their sentences.

How can I capitalize the first letter of each word in a string in C?

Today we will learn how to code a C program on how to Capitalize first and last letter of each word of a string i.e. to convert first and last letter of each word into uppercase . We will use toupper() function in the process. The toupper() function is used to convert lowercase alphabet to uppercase.

How do you capitalize the first word in 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.


2 Answers

Here is another go at it:

NSString *txt = @"hi my friends!";
txt = [txt stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[txt substringToIndex:1] uppercaseString]];

For Swift language:

txt.replaceRange(txt.startIndex...txt.startIndex, with: String(txt[txt.startIndex]).capitalizedString)
like image 71
Johan Kool Avatar answered Oct 16 '22 10:10

Johan Kool


The accepted answer is wrong. First, it is not correct to treat the units of NSString as "characters" in the sense that a user expects. There are surrogate pairs. There are combining sequences. Splitting those will produce incorrect results. Second, it is not necessarily the case that uppercasing the first character produces the same result as capitalizing a word containing that character. Languages can be context-sensitive.

The correct way to do this is to get the frameworks to identify words (and possibly sentences) in the locale-appropriate manner. And also to capitalize in the locale-appropriate manner.

[aMutableString enumerateSubstringsInRange:NSMakeRange(0, [aMutableString length])
                                   options:NSStringEnumerationByWords | NSStringEnumerationLocalized
                                usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    [aMutableString replaceCharactersInRange:substringRange
                                  withString:[substring capitalizedStringWithLocale:[NSLocale currentLocale]]];
    *stop = YES;
}];

It's possible that the first word of a string is not the same as the first word of the first sentence of a string. To identify the first (or each) sentence of the string and then capitalize the first word of that (or those), then surround the above in an outer invocation of -enumerateSubstringsInRange:options:usingBlock: using NSStringEnumerationBySentences | NSStringEnumerationLocalized. In the inner invocation, pass the substringRange provided by the outer invocation as the range argument.

like image 31
Ken Thomases Avatar answered Oct 16 '22 08:10

Ken Thomases