简体   繁体   English

救命! Obj-C / Iphone编程:从html文本中提取字符串并逐行读取

[英]Help! Obj-C/Iphone programming: extracting string from html text and reading off line by line

hihi, 嗨,

I have this html text response from a particular website: 我有来自特定网站的html文本回复:

<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="crsdsdfs2221.jpg">crash-2221.jpg</a></td><td align="right">14-Jun-2010 14:29  

Notice for every line, there is this href="_____.___", which is an image file with random name and random format. 注意,每一行都有一个href =“ _____.___”,这是一个具有随机名称和随机格式的图像文件。 I would like to extract that string within the inverted commas out so that i can append it into a URL path and download the image. 我想在反引号内提取该字符串,以便可以将其附加到URL路径并下载图像。 I've been looking through this documentation from apple: 我一直在看苹果的这份文件:

http://developer.apple.com/mac/library/documentation/cocoa/conceptual/strings/Articles/SearchingStrings.html#//apple_ref/doc/uid/20000149-CJBBGBAI http://developer.apple.com/mac/library/documentation/cocoa/conceptual/strings/Articles/SearchingStrings.html#//apple_ref/doc/uid/20000149-CJBBGBAI

on String programming but couldn't find one that fits my bill. 关于String编程,但找不到适合我的帐单。 Also after reading it, what code can I use to ensure that I will be reading the next line the next time my function is called( because I want to download the next picture). 同样在阅读完之后,我可以使用什么代码来确保下次调用函数时可以阅读下一行(因为我想下载下一张图片)。 Hope some kind soul can help me out, thanks! 希望有一个善良的灵魂可以帮助我,谢谢!

Four methods in the NSString class can get you the info you want. NSString类中的四个方法可以为您提供所需的信息。

  • componentsSeparatedByString: is how you will separate your responses by line. componentsSeparatedByString:是如何按行分隔响应的方式。 Give it a string parameter that occurs once per line and then examine each component individually. 给它一个字符串参数,该参数每行出现一次,然后分别检查每个组件。
  • rangeOfString: can find the indicies of specific substrings that occur in your string. rangeOfString:可以找到出现在字符串中的特定子字符串的rangeOfString: Use it to get the index of strings that you know will be there, like @"img src=\\"" and @"a href=\\"" 用它来获取您知道的字符串索引,例如@“ img src = \\”“和@” a href = \\“”
  • substringFromIndex: and substringToIndex: Use these in conjunction with rangeOfString to surgically subtract the information you need. substringFromIndex:substringToIndex:将它们与rangeOfString结合使用,以外科方式减去所需的信息。 The fact that the filenames you want are between quotation marks will be a big help in your case. 所需文件名位于引号之间的事实将对您有很大帮助。

Hope this leads you in the right direction. 希望这会指引您正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM