Divide 1 NSString into two NSStrings by whiteSpace
I have NSStringone that originally looked like <a href="http://link.com"> LinkName</a>. I removed the html tags and now has NSStringone that looks like
http://Link.com SiteName
how can I divide these two into different ones NSStringso that I have
http://Link.com
and
SiteName
I specifically want to show SiteNamein the label and just use http://Link.comto open in UIWebView, but I can’t when all is one line. Any suggestions or help are appreciated.
NSString *s = @"http://Link.com SiteName";
NSArray *a = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"http: '%@'", [a objectAtIndex:0]);
NSLog(@"site: '%@'", [a lastObject]);
NSLog Output:
http: 'http://Link.com'
site: 'SiteName'
Bonus, handle the site name with embedded space with RE:
NSString *s = @"<a href=\"http://link.com\"> Link Name</a>";
NSString *pattern = @"(http://[^\"]+)\">\\s+([^<]+)<";
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:s options:0 range:NSMakeRange(0, s.length)];
NSString *http = [s substringWithRange:[textCheckingResult rangeAtIndex:1]];
NSString *site = [s substringWithRange:[textCheckingResult rangeAtIndex:2]];
NSLog(@"http: '%@'", http);
NSLog(@"site: '%@'", site);
NSLog Output:
http: 'http://link.com'
site: 'Link Name'