温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

对URL字符的截取

发布时间:2020-07-31 17:50:52 来源:网络 阅读:333 作者:jna_114 栏目:开发技术

例如,对新浪微博的来源

<a href="http://weibo.com/" rel="nofollow">微博 weibo.com</a>

进行截取

下面介绍了三种方式 

//对微博来源字符串进行截取

        方法一,直接使用字符串的截取方式,不过此方法在遇到微博来源为空时程序会崩溃,所以要进行安全判断


      NSRange range1 = [self.source rangeOfString:@">"];

     self.source =  [self.source substringFromIndex:range1.location + 1];

       NSRange range2 = [self.source rangeOfString:@"<"];

       self.source = [self.source substringToIndex:range2.location];


        //方法二----正则表达式

        NSString *regex = @">[.\\w\\s]+<";//寻找><中间的任意字符()

        NSRegularExpression *regular = [[NSRegularExpression alloc]initWithPattern:regex options:0 error:nil];

      NSArray *arr = [regular matchesInString:self.source options:0 range:NSMakeRange(0, self.source.length)];

       

      if (arr.count > 0) {

            NSTextCheckingResult *result = arr[0];

            NSRange range = result.range;

            range.location += 1;

           range.length -= 2;

           self.source = [self.source substringWithRange:range];

       }

        

        //方法三---导入第三方框架RegexKitLite.h

        NSArray *arr = [self.source componentsMatchedByRegex:regex];

        if (arr.count > 0) {

            NSString *str = arr[0];

            NSRange range = {1,str.length-2};

            self.source = [str substringWithRange:range];

        }

        



对时间的转换

将时间格式为

Tue Sep 15 23:19:39 +0800 2015

转换成 09-15 23:19格式或自定义的时间格式(这要看你所需要的什么时间格式了)

 NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];

        [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];

//        [inputFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];

        [inputFormatter setDateFormat:@"EEE, MMM d HH:mm:ss Z yyyy"];

        NSDate* inputDate = [inputFormatter dateFromString:self.created_at];

        

        //格式化日期类

        NSDateFormatter *df = [[NSDateFormatter alloc] init];

        [df setDateFormat:@"MM-dd  HH:mm "];

        

        //将日期按照格式化类型转换成字符串

        self.created_at = [df stringFromDate:inputDate];



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI