通过whiteSpace将1个NSString分成两个NSStrings

编程入门 行业动态 更新时间:2024-10-22 17:20:41
本文介绍了通过whiteSpace将1个NSString分成两个NSStrings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有 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

本文发布于:2023-11-26 00:39:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   whiteSpace   NSString   NSStrings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!