2012-08-29 65 views
0

我有兩個字符串中有兩個對象:如何正確減少字符串?

<div align="center"><img src="http://farm9.staticflickr.com/8448/7882675644_76605a2a3d_b.jpg" border="0" alt="" /></div>< 

<img src="http://farm9.staticflickr.com/8425/7881940452_d2a8e898a3_o.png" border="0" alt="" /><br />< 

而且我試圖鏈接。減去圖像。

我得到的鏈接使用對象方法:

NSMutableString *string = [NSMutableString stringWithString:description]; 

int left = [string rangeOfString:@"http://"].location; 

int right = 0; 

if ([string rangeOfString:@".jpg"].location != NSNotFound) { 
    right = [string rangeOfString:@".jpg"].location; 
} 
else if ([string rangeOfString:@".png"].location != NSNotFound){ 
    right = [string rangeOfString:@".png"].location; 
} 


NSString *sub = [string substringWithRange:NSMakeRange(left, right)]; 

NSLog(@"%@",sub); 

但問題是,當我打印了我。減去:

2012-08-29 18:53:30.716 MyApple[56335:c07] http://farm9.staticflickr.com/8448/7882675644_76605a2a3d_b.jpg" border="0" alt="" /></di 
2012-08-29 18:53:30.717 MyApple[56335:c07] http://farm9.staticflickr.com/8425/7881940452_d2a8e898a3_o.png" bord 

IMO我從http。減去://以.jpg或。 PNG,但它不能正常工作。

感謝您的幫助。

回答

3

NSMakeRange()的第二個參數是長度,所以你可能需要

NSString *sub = [string substringWithRange:NSMakeRange(left, right - left)]; 

你也應該看看NSRegularExpression

+0

感謝您的回覆。我在同一時間解決它 –

+0

@TomaszSzulc:是的,這只是一個經常發生的微不足道的錯誤。 - 但是,無論如何,您應該「接受」答案,以便將問題從「未答覆」列表中刪除。 –