2013-07-28 218 views
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. 

回答

1

你應該從字符串中提取URL,然後格式化的方式顯示出來。

提取URL的簡單方法是正則表達式(RegEX)。

提取網址後,您可以用什麼替代它:

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

您可以使用此: https://stackoverflow.com/a/9587987/305135

+0

我已經提取了網址,我如何比較提取的網址與myString並只顯示說明? – Siva

+0

搜索你的字符串,並用[無]取代[提取的網址]! – AVEbrahimi

+0

目標C中是否有任何函數用於搜索和替換? – Siva

0

如果描述字符串由換行符(\ n)的分離,你可以這樣做:

NSArray *items = [yourString componentsSeparatedByString:@"\n"]; 
0

正則表達式是一個好主意。

但是,目標C中存在一個檢測字符串中URL的默認方式,NSDataDetectorNSDataDetector內部使用正則表達式。

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:@""]; 
}