提取锚标签的内容 [英] Extract Contents of Anchor Tag
本文介绍了提取锚标签的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的是提取存储在NSString中的锚标签的内容。
What I'm trying to do is extract the contents of a an anchor tag being stored in an NSString.
如果例如我有一个字符串, :
If for example I have a string with the following:
<a href="/url?q=https://kindle.amazon.com/&sa=U&ei=GdiWT5uCEI6BhQfihoTzDQ&ved=0CCUQFjAB&usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw">Amazon <b>Kindle</b>: Welcome</a>
我如何提取锚标签的内容,这样我会有以下: / p>
How would I go about extracting the contents of the anchor tag so that I would have the following:
https://kindle.amazon.com/&sa=U&ei=GdiWT5uCEI6BhQfihoTzDQ&ved=0CCUQFjAB&usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw
任何帮助,将不胜感激。
Any help would be greatly appreciated!
我完全厌烦,而这应该很简单?
I'm completely stumped, whereas this should be quite simple? The answer posted below keeps returning null.
推荐答案
一个可能的解决方案是使用NSScanner -
One Possible solution is by using NSScanner -
NSString *urlString = nil;
NSString *htmlString = @"<a href=\"/url?q=https://kindle.amazon.com/&sa=U&ei=GdiWT5uCEI6BhQfihoTzDQ&ved=0CCUQFjAB&usg=AFQjCNEoRolsgoynLNS0H60VWz-9EaQdtw\">Amazon <b>Kindle</b>: Welcome</a>";
NSScanner *scanner = [NSScanner scannerWithString:htmlString];
[scanner scanUpToString:@"<a" intoString:nil];
if (![scanner isAtEnd]) {
[scanner scanUpToString:@"http" intoString:nil];
NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@">"];
[scanner scanUpToCharactersFromSet:charset intoString:&urlString];
}
NSLog(@"%@", urlString);
这篇关于提取锚标签的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文