我有这种格式的输出字符串。 我需要格式化字符串,以便我可以单独显示URL和我的内容,分别说明。有没有任何功能,所以我可以轻松地格式化它们? 代码:
NSLog(@"Description %@", string);
OUTPUT字符串:
2013-07-28 11:13:59.083 RSSreader[4915:c07] Description
http://www.apple.com/pr/library/2013/07/23Apple-Reports-Third-Quarter-Results.html?sr=hotnews.rss
Apple today announced financial results for its fiscal 2013 third quarter ended
June 29, 2013. The Company posted quarterly revenue of $35.3 billion and quarterly
net profit of $6.9 billion, or $7.47 per diluted share.
Apple sold 31.2 million iPhones, which set a June quarter record.
答案 0 :(得分:1)
您应该从字符串中提取URL,然后以格式化方式显示它。
提取网址的一种简单方法是正则表达式( RegEX )。
提取网址后,您可以将其替换为任何内容:
str = [str stringByReplacingOccurrencesOfString:extractedURL
withString:@""];
答案 1 :(得分:0)
如果描述字符串由换行符(\ n)分隔,则可以执行以下操作:
NSArray *items = [yourString componentsSeparatedByString:@"\n"];
答案 2 :(得分:0)
正则表达式是个好主意。
但是有一种默认的方法可以检测Objective C中的字符串中的URL NSDataDetector
。
NSDataDetector
内部使用正则表达式。
NSString *aString = @"YOUR STRING WITH URLs GOES HERE"
NSDataDetector *aDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *theMatches = [aDetector matchesInString:aString options:0 range:NSMakeRange(0, [aString length])];
for (int anIndex = 0; anIndex < [theMatches count]; anIndex++) {
// If needed, Save this URL for Future Use.
NSString *anURLString = [[[theMatches objectAtIndex:anIndex] URL] absoluteString];
// Replace the Url with Empty String
aTitle = [aTitle stringByReplacingOccurrencesOfString:anURLString withString:@""];
}