通过空格将 1 个 NSString 分成两个 NSString

发布于 2024-12-09 06:51:34 字数 457 浏览 0 评论 0原文

我有一个 NSString ,最初看起来像 链接名称。我删除了 html 标签,现在有一个 NSString ,看起来

http://Link.com   SiteName

如何将两者分成不同的 NSString ,这样我就可以

http://Link.com

并且

SiteName

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

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

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

http://Link.com

and

SiteName

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2024-12-16 06:51:34
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'

带有嵌入空格的站点名称:

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);

额外的好处,使用 RE: NSLog 输出处理

http: 'http://link.com'
site: 'Link Name'
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 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 output:

http: 'http://link.com'
site: 'Link Name'
轻拂→两袖风尘 2024-12-16 06:51:34

NSString 有一个带有签名的方法:

componentsSeparatedByString:

它返回一个组件数组作为其结果。像这样使用它:

NSArray *components = [myNSString componentsSeparatedByString:@" "];

[components objectAtIndex:0]; //should be SiteName
[components objectAtIndex:1]; // should be http://Link.com

祝你好运。

NSString has a method with the signature:

componentsSeparatedByString:

It returns an array of components as its result. Use it like this:

NSArray *components = [myNSString componentsSeparatedByString:@" "];

[components objectAtIndex:0]; //should be SiteName
[components objectAtIndex:1]; // should be http://Link.com

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文