我有 NSString ,最初看起来像< a href =link> LINKNAME< / A> 。我删除了html标签,现在有一个 NSString 看起来像
I have an NSString which initially looked like <a href="link"> LinkName</a>. I removed the html tags and now have an NSString that looks like
Link SiteName如何将两者分成不同的 NSString s所以我会
how can I separate the two into different NSStrings so I would have
Link和
SiteName我特别想在 SiteName 中显示一个标签,只需使用 Link 在 UIWebView 中打开,但我不能在这是一个字符串。非常感谢任何建议或帮助。
I specifically want to show the SiteName in a label and just use the Link to open in a UIWebView but I can't when it is all one string. Any suggestions or help is greatly appreciated.
推荐答案NSString *s = @"Link SiteName"; NSArray *a = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSLog(@"http: '%@'", [a objectAtIndex:0]); NSLog(@"site: '%@'", [a lastObject]);
NSLog输出:
http: 'Link' site: 'SiteName'奖金,处理带有RE的嵌入空间的网站名称:
Bonus, handle a site name with an embedded space with a RE:
NSString *s = @"<a href=\"link\"> Link Name</a>"; NSString *pattern = @"([^\"]+)\">\\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输出:
http: 'link' site: 'Link Name'更多推荐
通过whiteSpace将1个NSString分成两个NSStrings
发布评论