Skip to content

Instantly share code, notes, and snippets.

@clsource
Last active Aug 29, 2015
Embed
What would you like to do?
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