给定一个大的字符串,创建一个包含在字符串中的所有有效url的数组的最佳方法是什么?Objective-C - 在字符串中查找URL
13
A
回答
41
对于此,无需使用RegexKitLite
,因为iOS 4 Apple提供NSDataDetector
(NSRegularExpression
的子类)。
你可以使用它只是像这样(source
是你的字符串):
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray* matches = [detector matchesInString:source options:0 range:NSMakeRange(0, [source length])];
6
我会使用RegexKitLite此:
#import "RegExKitLite.h"
...
NSString * urlString = @"blah blah blah http://www.google.com blah blah blah http://www.stackoverflow.com blah blah balh http://www.apple.com";
NSArray *urls = [urlString componentsMatchedByRegex:@"http://[^\\s]*"];
NSLog(@"urls: %@", urls);
打印:
urls: (
"http://www.google.com",
"http://www.stackoverflow.com",
"http://www.apple.com"
)
(当然,我用那里的URL正则表达式的简化,但你的想法。 )
-1
这是提取URL链接最好的办法。
NSString *url_ = @"dkc://name.com:8080/123;param?id=123&pass=2#fragment";
NSURL *url = [NSURL URLWithString:url_];
NSLog(@"scheme: %@", [url scheme]);
NSLog(@"host: %@", [url host]);
NSLog(@"port: %@", [url port]);
NSLog(@"path: %@", [url path]);
NSLog(@"path components: %@", [url pathComponents]);
NSLog(@"parameterString: %@", [url parameterString]);
NSLog(@"query: %@", [url query]);
NSLog(@"fragment: %@", [url fragment]);
输出:
方案:DKC
主机:name.com
端口:8080
路径:/ 12345
路径组成部分:( “/”, 123)parameterString:PARAM
查询:ID = 1个&通= 2
片段:片段
相关问题
- 1. 在字符串中查找URL
- 2. 在URL中查找字符串 - joomla/jQuery
- 3. 在字符串中查找图片url
- 4. 从URL中查找子字符串
- 5. 查找字符串中的URL
- 6. 在字符串中查找字符串
- 7. 在字符串中查找字符串
- 8. 在字符串中查找字符
- 9. 查找字符串中的字符串
- 10. 查找字符串中的字符串
- 11. 查找字符串中的字符串
- 12. 结合字符串中的ObjectiveC
- 13. 在URL查询字符串
- 14. 在运行时将ObjectiveC字符串字符串化
- 15. 在字符串C++中查找子字符串(在“hello”中查找“el”)
- 16. 在输入字符串中查找字符串中的子串
- 17. 在字符串中查找数字
- 18. 在字符串lua中查找数字
- 19. 在字符串中查找数字
- 20. Rails在URL中查询字符串?
- 21. 查找字符串中的
- 22. URL查询字符串在字符串中有一个逗号
- 23. 使用js在url中查找字符串
- 24. Knockout.js数据绑定在URL中查找部分字符串
- 25. PHP在字符串中查找任何形式的URL
- 26. 查找字符串
- 27. 查找字符串
- 28. 查找字符串
- 29. 查找字符串
- 30. 查找字符串
这是用于显示在一个'UITextView'或'UILabel'或soemthing别的? – dredful 2011-05-14 00:37:59
请参阅[我的答案](http://stackoverflow.com/a/25574255/1966109)了解使用Swift 3的类似问题,并提供了2种方法来解决您的问题。 – 2017-05-08 20:43:46