NSString中解析URL
来源:互联网 发布:js只允许输入数字 编辑:程序博客网 时间:2024/05/28 11:29
总结几种方法达到这种目的。
1.正则表达式法。
- NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"(?i)//b((?:[a-z][//w-]+:(?:/{1,3}|[a-z0-9%])|www//d{0,3}[.]|[a-z0-9.//-]+[.][a-z]{2,4}/)(?:[^//s()<>]+|//(([^//s()<>]+|(//([^//s()<>]+//)))*//))+(?://(([^//s()<>]+|(//([^//s()<>]+//)))*//)|[^//s`!()//[//]{};:'/".,<>?«»“”‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];
- NSString *someString = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it.";
- NSString *match = [someString substringWithRange:[expression rangeOfFirstMatchInString:someString options:NSMatchingCompleted range:NSMakeRange(0, [someString length])]];
- NSLog(@"%@", match); // Correctly prints 'http://abc.com/efg.php?EFAei687e3EsA'
至于这个正则表达式,是从网上找的,请看这儿。
2.系统的NSDataDetector
- NSString *string = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it.";
- NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
- NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
- for (NSTextCheckingResult *match in matches) {
- if ([match resultType] == NSTextCheckingTypeLink) {
- NSURL *url = [match URL];
- NSLog(@"found URL: %@", url);
- }
- }
3.直接操作NSString
- NSURL *url;
- NSMutableArray *listItems = [[someString componentsSeparatedByString:@" "] mutableCopy];
- for(int i=0;i<[listItems count];i++)
- {
- NSString *str=[listItems objectAtIndex:i];
- if ([str rangeOfString:@"http://"].location == NSNotFound)
- NSLog(@"Not url");
- else
- url=[NSURL URLWithString:str];
- }
- NSString中解析URL
- NSString中解析URL .
- NSString中解析URL
- NSString中解析URL
- NSString 中提取URL
- 在NSString中 添加URL 图片
- NSString 中提取url 并打开
- J2EE 中 URL 解析
- 解析url中search
- @implementation NSString (URL)
- OC中NSString/NSArray/#define宏定义的解析
- iOS NSString创建解析
- *NSString 内存存储解析
- ThinkPHP中URL解析原理,以及URL路由使用教程!
- URL拼接规则 js解析URL中参数
- 解析url中各个参数的值
- Android中解析网络请求的URL
- url中有中文参数后台解析
- Qt d指针简单实现及解析--威力加强版
- 普通PC键盘和苹果键盘比较图
- 基于钩子的改键
- GUI程序设计
- 网页中、英文安全字体选择及设置
- NSString中解析URL
- 跳转到上一个页面
- 堆栈
- 创建控件
- linux开机启动服务和chkconfig使用方法
- GUI程序设计
- php处理全局变量
- android.view.ViewGroup.LayoutParams介绍
- 5 simple ways to troubleshoot using Strace