Sunday, June 2, 2013

NSMutableString replaceOccurrencesOfString withString options range example in Objective C (iOS).


NSMutableString replaceOccurrencesOfString withString options range

Replaces all occurrences of a given string in a given range with another given string, returning the number of replacements.

- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)opts range:(NSRange)searchRange

Parameters of [NSMutableString replaceOccurrencesOfString withString options range]
target
The string to replace.
Important: Raises an NSInvalidArgumentException if target is nil.
replacement
The string with which to replace target.
Important: Raises an NSInvalidArgumentException if replacement is nil.
opts
A mask specifying search options. See String Programming Guide for details.
If opts is NSBackwardsSearch, the search is done from the end of the range. If opts is NSAnchoredSearch, only anchored (but potentially multiple) instances are replaced. NSLiteralSearch and NSCaseInsensitiveSearch also apply.
searchRange
The range of characters to replace. aRange must not exceed the bounds of the receiver. Specify searchRange as NSMakeRange(0, [receiver length]) to process the entire string.
Important: Raises an NSRangeException if any part of searchRange lies beyond the end of the receiver.

Return Value of [NSMutableString replaceOccurrencesOfString withString options range]
The number of replacements made.

Discussion of [NSMutableString replaceOccurrencesOfString withString options range]
This method treats the length of the string as a valid range value that returns an empty string.
NSMutableString replaceOccurrencesOfString withString options range example.
-(void)DoStuff
{
NSString *aString [ [[NSString alloc] initWithFormat:@"text %@ more text", self.strVariable];
[self.someMutableStringVar replaceOccurrencesOfString:@"replace" withString:aString options:NSCaseInsensitiveSearch range:NSMakeRange(0, [self.someMutableStringVar length])];
[aString release];
}

-(void)DoStuff2:(NSString *)aString
{
[self.someMutableStringVar appendString:aString];
}

Example of [NSMutableString replaceOccurrencesOfString withString options range].
NSMutableString * theMutableString = [[NSMutableString alloc] initWithString:@"\"Hello World!\""];
NSLog(@"%@",theMutableString);

[theMutableString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:(NSRange){0,[theMutableString length]}];

NSLog(@"%@",theMutableString);

[theMutableString release];

NSMutableString replaceOccurrencesOfString withString options range example.
 (NSMutableString *)fixLinkedFiledPath:(NSString *)aPath {
        NSMutableString *fixedPathOfFile = [NSMutableString stringWithString:aPath];
        NSString *dataFolderPath = [[NSApp delegate] dataFolderPath];
   [fixedPathOfFile replaceOccurrencesOfString:@"[Data Folder]" withString:dataFolderPath options:NSLiteralSearch range:NSMakeRange(0,[fixedPathOfFile length])];
}
...
NSString *pathURL = [self fixLinkedFiledPath:[link valueForKey:@"url"]];  //Somewhere in your code

End of NSMutableString replaceOccurrencesOfString withString options range example article.