0

我有一个这种格式的输出字符串。我需要格式化字符串,以便我可以分别显示 URL 和我的内容、描述。是否有任何功能,所以我可以轻松地格式化它们?编码 :

            NSLog(@"Description %@", string);

输出字符串:

    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. 
4

3 回答 3

1

您应该从字符串中提取 URL,然后以格式化的方式显示它。

提取 URL 的一种简单方法是正则表达式 ( RegEX )。

提取 URL 后,您可以将其替换为空:

str = [str stringByReplacingOccurrencesOfString:extractedURL
                                     withString:@""];

你可以使用这个: https ://stackoverflow.com/a/9587987/305135

于 2013-07-28T05:59:18.670 回答
0

如果描述字符串由换行符 (\n) 分隔,您可以这样做:

NSArray *items = [yourString componentsSeparatedByString:@"\n"];
于 2013-07-28T06:51:01.183 回答
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:@""];
}
于 2013-07-28T08:51:20.467 回答