Iphone 提取字符串的一部分

Iphone 提取字符串的一部分,iphone,ios,xcode,substring,href,Iphone,Ios,Xcode,Substring,Href,我的应用程序从RSS源获取预览图像的字符串。它以这种格式显示: <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/2

我的应用程序从RSS源获取预览图像的字符串。它以这种格式显示:

<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和RayWenderlich博客上的教程解析rss。我将NSString设置为RSS中为图像给定的值。然后我用这个字符串设置了一个NSLog。NSLog返回的是我在原始帖子中的内容。有没有一种方法可以将其子串,只得到“”之间的部分?

看看苹果的文档。您应该能够将字符串用作NSData并使用该对象对其进行解析。

使用NSRange,但需要确定起点和长度(两个引号的位置)

[未测试的代码]
int idStart=0;
int-idEnd=0;
对于(int f=0;f<[初始字符串长度];f++){
NSRange myRangeStart;
myRangeStart.location=f;
myRangeStart.length=1;
substr=[urlStr substringWithRange:myRangeStart];
如果(idStart==0){
if([substr isEqualToString:@“\”“]”){
idStart=f;
}
}否则{
if([substr IsequalString:@“\”]&&f>idStart){
idEnd=f;
}
}
}
NSString*子字符串=@;
NSRange-myRange;
myRange.location=idStart+1;
myRange.length=idEnd-idStart-1;
substring=[initialString substringWithRange:myRange];
[/未测试代码]

正如NTTake的意思一样,您只需要解析XML即可。此外,似乎您的“提要”格式不正确,它被声明为一行,但您的右括号也在其中。因此,它可能无法正确解析。我正在使用GDATAXML和Ray Wenderlich博客上的教程解析它。我将NSString设置为RSS中为图像给定的值。然后我用这个字符串设置了一个NSLog。NSLog返回的是我在原始帖子中的内容。有没有一种方法可以对它进行子串,从而只得到“”之间的部分?我想应该这样说,因为我已经在解析XML了,不想解析它。
[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]