Skip to content

Instantly share code, notes, and snippets.

@clsource
Last active August 29, 2015 14:07
Show Gist options
  • Save clsource/f41e7b93b6861734ef12 to your computer and use it in GitHub Desktop.
Save clsource/f41e7b93b6861734ef12 to your computer and use it in GitHub Desktop.
Implement a Substring Closure within a Range in Objective C
// Substring Closure
// Similar for what found in other languages
// Usage
// substring(@"my-string", 2, 4);
// => -st
NSString * ( ^ substring ) (NSString *, int , int);
substring = ^(NSString * string, int from, int to) {
NSString * digit = @"";
NSMutableString * sub = [@"" mutableCopy];
for (int i = from; i < to; i++) {
digit = [NSString stringWithFormat:@"%c", [string characterAtIndex:i]];
[sub appendString:digit];
}
NSLog(@"Substring %@ From %d To %d Result %@ ", string, from, to, sub);
return sub;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment