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

后端 未结 9 526
青春惊慌失措
青春惊慌失措 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()
    
    0 讨论(0)
  • 2021-01-30 09:32

    I know the question asks specifically for an Objective C answer, however here is a solution for Swift 2.0:

    let txt = "hi my friends!"
    var sentencecaseString = ""
    
    for (index, character) in txt.characters.enumerate() {
        if 0 == index {
            sentencecaseString += String(character).uppercaseString
        } else {
            sentencecaseString.append(character)
        }
    }
    

    Or as an extension:

    func sentencecaseString() -> String {
        var sentencecaseString = ""
        for (index, character) in self.characters.enumerate() {
            if 0 == index {
                sentencecaseString += String(character).uppercaseString
            } else {
                sentencecaseString.append(character)
            }
        }
        return sentencecaseString
    }
    
    0 讨论(0)
  • 2021-01-30 09:33
    pString = [pString
               stringByReplacingCharactersInRange:NSMakeRange(0,1)
               withString:[[pString substringToIndex:1] capitalizedString]];
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-30 09:46

    Use

    - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
    

    and capitalize the first object in the array and then use

    - (NSString *)componentsJoinedByString:(NSString *)separator
    

    to join them back

    0 讨论(0)
  • 2021-01-30 09:46

    you can user with regular expression i have done it's works for me simple you can paste below code +(NSString*)CaptializeFirstCharacterOfSentence:(NSString*)sentence{

    NSMutableString *firstCharacter = [sentence mutableCopy];
    NSString *pattern = @"(^|\\.|\\?|\\!)\\s*(\\p{Letter})";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];
    [regex enumerateMatchesInString:sentence options:0 range:NSMakeRange(0, [sentence length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        //NSLog(@"%@", result);
        NSRange r = [result rangeAtIndex:2];
        [firstCharacter replaceCharactersInRange:r withString:[[sentence substringWithRange:r] uppercaseString]];
    }];
    NSLog(@"%@", firstCharacter);
    return firstCharacter;
    

    } //Call this method NsString *resultSentence = [UserClass CaptializeFirstCharacterOfSentence:yourTexthere];

    0 讨论(0)
提交回复
热议问题