Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first letter of every sentence in NSString

How can I capitalize the first letter of each sentence in an NSString? For example, the string: @"this is sentence 1. this is sentence 2! is this sentence 3? last sentence here." should become: @"This is sentence 1. This is sentence 2! Is this sentence 3? Last sentence here."

like image 798
amirfl Avatar asked Mar 20 '13 04:03

amirfl


3 Answers

static NSString *CapitalizeSentences(NSString *stringToProcess) {
    NSMutableString *processedString = [stringToProcess mutableCopy];


    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];


    // Ironically, the tokenizer will only tokenize sentences if the first letter
    // of the sentence is capitalized...
    stringToProcess = [stringToProcess uppercaseStringWithLocale:locale];


    CFStringTokenizerRef stringTokenizer = CFStringTokenizerCreate(kCFAllocatorDefault, (__bridge CFStringRef)(stringToProcess), CFRangeMake(0, [stringToProcess length]), kCFStringTokenizerUnitSentence, (__bridge CFLocaleRef)(locale));


    while (CFStringTokenizerAdvanceToNextToken(stringTokenizer) != kCFStringTokenizerTokenNone) {
        CFRange sentenceRange = CFStringTokenizerGetCurrentTokenRange(stringTokenizer);

        if (sentenceRange.location != kCFNotFound && sentenceRange.length > 0) {
            NSRange firstLetterRange = NSMakeRange(sentenceRange.location, 1);

            NSString *uppercaseFirstLetter = [[processedString substringWithRange:firstLetterRange] uppercaseStringWithLocale:locale];

            [processedString replaceCharactersInRange:firstLetterRange withString:uppercaseFirstLetter];
        }
    }


    CFRelease(stringTokenizer);


    return processedString;
}
like image 67
fumoboy007 Avatar answered Nov 15 '22 21:11

fumoboy007


Use

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

put all the separator(? ,. ,! ) from which you expect beginning of new sentence, make sure to put back the actual separator and capitalize the first object in the array and then use

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

to join them back with space separator

for capitalizing the first letter of each sentence run for loop for all elements of the array.

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

like image 1
Manish Agrawal Avatar answered Nov 15 '22 20:11

Manish Agrawal


This seems to work:

NSString *s1 = @"this is sentence 1. this is sentence 2! is this sentence 3? last sentence here.";

NSMutableString *s2 = [s1 mutableCopy];
NSString *pattern = @"(^|\\.|\\?|\\!)\\s*(\\p{Letter})";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];
[regex enumerateMatchesInString:s1 options:0 range:NSMakeRange(0, [s1 length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    //NSLog(@"%@", result);
    NSRange r = [result rangeAtIndex:2];
    [s2 replaceCharactersInRange:r withString:[[s1 substringWithRange:r] uppercaseString]];
}];
NSLog(@"%@", s2);
// This is sentence 1. This is sentence 2! Is this sentence 3? Last sentence here.
  • "(^|\\.|\\?|\\!)" matches the start of the string or ".", "?", or "!",
  • "\\s*" matches optional white space,
  • "(\\p{Letter})" matches a letter character.

So this pattern finds the first letter of each sentence. enumerateMatchesInString enumerates all the matches and replaces the occurrence of the letter by the upper case letter.

like image 1
Martin R Avatar answered Nov 15 '22 20:11

Martin R