Fre*_*pl3 5 iphone whitespace nsstring
我有一个NSString最初的样子<a href="http://link.com"> LinkName</a>.我删除了html标签,现在NSString看起来像
http://Link.com SiteName
Run Code Online (Sandbox Code Playgroud)
我怎么能把这两个分成不同的NSStrings,所以我会
http://Link.com
Run Code Online (Sandbox Code Playgroud)
和
SiteName
Run Code Online (Sandbox Code Playgroud)
我特别想SiteName在标签中显示并只使用http://Link.com在一个打开UIWebView但我不能当它是一个字符串.非常感谢任何建议或帮助.
NSString *s = @"http://Link.com SiteName";
NSArray *a = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"http: '%@'", [a objectAtIndex:0]);
NSLog(@"site: '%@'", [a lastObject]);
Run Code Online (Sandbox Code Playgroud)
NSLog输出:
http: 'http://Link.com'
site: 'SiteName'
Run Code Online (Sandbox Code Playgroud)
奖金,处理具有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);
Run Code Online (Sandbox Code Playgroud)
NSLog输出:
http: 'http://link.com'
site: 'Link Name'
Run Code Online (Sandbox Code Playgroud)