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

后端 未结 9 632
青春惊慌失措
青春惊慌失措 2021-01-30 08:54

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];
         


        
9条回答
  •  我在风中等你
    2021-01-30 09:35

    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)
    

提交回复
热议问题