You can list the words of a string and change each word separately. This works even if words are separated by other characters than the whitespace character:
NSString *str = @"dino mcCool. foo-bAR"; NSMutableString *result = [str mutableCopy]; [result enumerateSubstringsInRange:NSMakeRange(0, [result length]) options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { [result replaceCharactersInRange:NSMakeRange(substringRange.location, 1) withString:[[substring substringToIndex:1] uppercaseString]]; }]; NSLog(@"%@", result);
Martin r
source share