字符串中的多个子字符串 [英] Multiple Substrings in String

查看:60
本文介绍了字符串中的多个子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个可显示来自网站的6张图片的应用程序.这些图片随时间变化,因此我提取了该网站的源代码,并设法使用以下代码提取了6张图片中的第一张:

I´m working on an app that displays 6 pictures from a website. These pictures change over time so I extracted the sourcecode of said website and managed to pull the first of the 6 pictures with this code:

NSError *error = nil;
NSString *deviantStringPopular;
deviantStringPopular = [NSString stringWithContentsOfURL:[NSURL     URLWithString:@"http://browse.deviantart.com/?order=24"]
                                         encoding:NSISOLatin1StringEncoding
                                            error:&error];
NSString *popularContent;
NSRange popularURLRange1 = [deviantStringPopular rangeOfString:@"super_img=\""];
NSRange popularURLRange2 = [deviantStringPopular rangeOfString:@"\" super_w"];
int lengt = popularURLRange2.location - popularURLRange1.location -     popularURLRange1.length;
int location = popularURLRange1.location + popularURLRange1.length;
NSRange endRange;
endRange.location = location;
endRange.length = lengt;
popularContent = [deviantStringPopular substringWithRange:endRange];
NSLog(@"%@", popularContent);

问题是,其他5个图像的URL与第一个图像位于相同的子字符串之间.那么一旦成功加载第一张图片的URL,然后第二张图片加载并存储在不同的变量下,是否有可能忽略该图片的URL?

The problem is, the other 5 images' URLs are between the same substrings as the first one. So is it possible that the first image's URL is ignored once the it´s loaded successfully and the second one loads and is stored under a different variable and so on?

提前感谢

推荐答案

要从字符串中获取6个URL并将其加载到不同的变量中,可以尝试使用循环.在每次迭代中,在找到子字符串并将其存储到变量之后,进行一个范围,从字符串的开头到找到的子字符串的结尾.您可以使用 stringByReplacingCharactersInRange 擦除已搜索字符串的一部分,以便下次在字符串中搜索子字符串时,它将找到下一个URL.

To get 6 URLs from the string and load them into different variables, you could try using a loop. With each iteration, after the substring is found and stored into a variable, make a range from the beginning of the string up to the end of the substring you found. You can use stringByReplacingCharactersInRange to erase the part of the string already searched so that the next time you search the string for the substring, it will find the next URL.

这篇关于字符串中的多个子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆