简体   繁体   中英

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:

<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. 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. 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

on String programming but couldn't find one that fits my bill. 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.

  • componentsSeparatedByString: is how you will separate your responses by line. 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. Use it to get the index of strings that you know will be there, like @"img src=\\"" and @"a href=\\""
  • substringFromIndex: and substringToIndex: Use these in conjunction with rangeOfString to surgically subtract the information you need. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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