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

后端 未结 9 540
青春惊慌失措
青春惊慌失措 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:32

    In swift you can do it as followed by using this extension:

    extension String {
        func ucfirst() -> String {
            return (self as NSString).stringByReplacingCharactersInRange(NSMakeRange(0, 1), withString: (self as NSString).substringToIndex(1).uppercaseString)
        }    
    }
    

    calling your string like this:

    var ucfirstString:String = "test".ucfirst()
    

提交回复
热议问题