例如,对新浪微博的来源

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