Skip to content

Instantly share code, notes, and snippets.

@Adam0101
Created January 13, 2011 19:14
Show Gist options
  • Save Adam0101/778415 to your computer and use it in GitHub Desktop.
Save Adam0101/778415 to your computer and use it in GitHub Desktop.
Example of character substitution using NSRegularExpression
- (NSString *)grabData:(NSString *)searchTerm {
// Setup an error to catch stuff in
NSError *error = NULL;
//Create the regular expression to match against
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\s" options:NSRegularExpressionCaseInsensitive error:&error];
// create the new string by replacing the matching of the regex pattern with the template pattern(whitespace)
NSString *newSearchString = [regex stringByReplacingMatchesInString:searchTerm options:0 range:NSMakeRange(0, [searchTerm length]) withTemplate:@"%20"];
NSLog(@"New string: %@",newSearchString);
return newSearchString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment