Created
July 13, 2013 13:08
-
-
Save prudnikov/5990693 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSString (Additions) | |
- (NSString *)stringByReplacingSubstringsAtRanges:(NSArray *) rangeValues | |
withStrings:(NSArray *)strings; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSString+Additions.h" | |
@implementation NSString (Additions) | |
- (NSString *)stringByReplacingSubstringsAtRanges:(NSArray *)rangeValues | |
withStrings:(NSArray *)strings | |
{ | |
NSMutableString *resultCode = [[self mutableCopy] autorelease]; | |
SMAssert(([rangeValues count] == [strings count]), @"Number of ranges and string replacements should be equal"); | |
NSInteger diff = 0; | |
NSRange replaceRange; | |
NSString *replaceString; | |
for (NSUInteger index =0; index < [rangeValues count]; index++) | |
{ | |
replaceRange = [[rangeValues objectAtIndex:index] rangeValue]; | |
replaceRange.location += diff; | |
replaceString = [strings objectAtIndex:index]; | |
[resultCode replaceCharactersInRange:replaceRange | |
withString:replaceString]; | |
diff += [replaceString length] - replaceRange.length; | |
} | |
return resultCode; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment