2012-05-14 58 views
0

我的應用程序從預覽圖像的RSS提要中獲取字符串。它顯示格式爲:提取部分NSString

<p><a href="http://316apps.com/LakesideNews/wp-content/uploads/2012/05/144x1443.png"><img class="alignnone size-full wp-image-22" title="144x144" src="http://316apps.com/LakesideNews/wp-content/uploads/2012/05/144x1443.png" alt="" width="144" height="144" /></a></p> 

我解析使用GDATAXML和教程雷Wenderlich的博客了RSS。我設置了一個NSString爲圖像的RSS中給出的值。然後我用這個字符串設置一個NSLog。 NSLog的回報是我在原文中提出的。有沒有一種方法來subString它只獲得「」之間的部分?

+0

作爲NTTake meantioned,你只需要解析XML。 此外,它似乎好像你的「飼料」是不正確的格式被宣佈爲一條線,但你也有在那裏的右括號。所以它可能無法正確解析 – CStreel

回答

1

使用NSRange,但你需要確定起點和長度(兩個引號的位置)

[untested code] 

int idStart = 0; 
int idEnd = 0; 

for (int f = 0; f < [initialString length]; f++) { 
    NSRange myRangeStart; 
    myRangeStart.location = f; 
    myRangeStart.length = 1; 
    substr = [urlStr substringWithRange:myRangeStart]; 
    if (idStart == 0) { 
     if ([substr isEqualToString:@"\""]) { 
      idStart = f; 
     } 
    } else { 
     if ([substr isEqualToString:@"\""] && f > idStart) { 
      idEnd = f; 
     } 
    } 
} 


NSString* substring = @""; 
NSRange myRange; 
myRange.location = idStart+1; 
myRange.length = idEnd-idStart-1; 
substring = [initialString substringWithRange:myRange]; 

[/untested code] 
1

請在Apple的文檔中查看NSXMLParser。您應該能夠使用該字符串作爲NSData並使用該對象進行分析。

+0

我使用GDATAXML和Ray Wenderlich博客的教程解析它。我設置了一個NSString爲圖像的RSS中給出的值。然後我用這個字符串設置一個NSLog。 NSLog的回報是我在原文中提出的。有沒有一種方法來subString它只獲得「」之間的部分? – user717452

+0

你說過了。 –

+0

想象它需要說,因爲我ALREADY解析XML,不想分析它。 – user717452