If you want to crop both sides and also write less code:
NSString *webAddress = @"http://www.google.co.nz"; // add prefixes you'd like to filter out here NSArray *prefixes = [NSArray arrayWithObjects:@"https:", @"http:", @"//", @"/", nil]; for (NSString *prefix in prefixes) if([webAddress hasPrefix:prefix]) webAddress = [webAddress stringByReplacingOccurrencesOfString:prefix withString:@"" options:NSAnchoredSearch range:NSMakeRange(0, [webAddress length])]; // add suffixes you'd like to filter out here NSArray *suffixes = [NSArray arrayWithObjects:@"/", nil]; for (NSString *suffix in suffixes) if([webAddress hasSuffix:suffix]) webAddress = [webAddress stringByReplacingOccurrencesOfString:suffix withString:@"" options:NSBackwardsSearch range:NSMakeRange(0, [webAddress length])];
This code will remove the specified prefixes from the front and suffixes from the back (as a trailing slash). Just add extra substrings to the prefix / suffix array to filter more.
Thomas verbeek
source share