通过whiteSpace将1个NSString分成两个NSStrings [英] Separate 1 NSString into two NSStrings by whiteSpace

查看:89
本文介绍了通过whiteSpace将1个NSString分成两个NSStrings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 NSString ,最初看起来像< a href =http://link.com> LINKNAME< / A> 。我删除了html标签,现在有一个 NSString 看起来像

I have an NSString which initially looked like <a href="http://link.com"> LinkName</a>. I removed the html tags and now have an NSString that looks like

http://Link.com   SiteName

如何将两者分成不同的 NSString s所以我会

how can I separate the two into different NSStrings so I would have

http://Link.com

SiteName

我特别想在 SiteName 中显示一个标签,只需使用 http://Link.com UIWebView 中打开,但我不能在这是一个字符串。非常感谢任何建议或帮助。

I specifically want to show the SiteName in a label and just use the http://Link.com to open in a UIWebView but I can't when it is all one string. Any suggestions or help is greatly appreciated.

推荐答案

NSString *s = @"http://Link.com   SiteName";
NSArray *a = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"http: '%@'", [a objectAtIndex:0]);
NSLog(@"site: '%@'", [a lastObject]);

NSLog输出:

http: 'http://Link.com'
site: 'SiteName'

奖金,处理带有RE的嵌入空间的网站名称:

Bonus, handle a site name with an embedded space with a 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输出:

http: 'http://link.com'
site: 'Link Name'

这篇关于通过whiteSpace将1个NSString分成两个NSStrings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆