Skip to content

Instantly share code, notes, and snippets.

@prudnikov
Created July 13, 2013 13:08
Show Gist options
  • Save prudnikov/5990693 to your computer and use it in GitHub Desktop.
Save prudnikov/5990693 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface NSString (Additions)
- (NSString *)stringByReplacingSubstringsAtRanges:(NSArray *) rangeValues
withStrings:(NSArray *)strings;
@end
#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