PDA

View Full Version : Index of Substring




MaDDaemoN
Dec 1, 2009, 03:49 PM
Hello, im a java developer, now trying to learn objective c. I got one problem while developing app. The concept of app is to download some html page and to parse values from tables.

On java it is easy to parse some substring:


Code:

You must Login or Register to view and contribute code! This is done to increase participation in helping one another out, if you have been helped please pass on the favor.
view plaincopy to clipboardprint?
s="<td>Hello world</td>";
s=s.substring(s.indexOf("<td>")+4);
s=s.substring(0,s.indexOf("</td>"));

So string s will contain Hello World.

But i did not find such method as substring in NSString class.
How can i do such task in Ob-C?



bredell
Dec 1, 2009, 04:10 PM
Take a look at the NSScanner class. Or read the "String Programming Guide for Cocoa".

MaDDaemoN
Dec 1, 2009, 04:19 PM
I`ve read Cocoa string article already, but i did not find it helpfull. NSScanner also does not provide claimed functionality.

dejo
Dec 1, 2009, 04:24 PM
I`ve read Cocoa string article already, but i did not find it helpfull.
Not even the Searching, Comparing, and Sorting Strings (http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Strings/Articles/SearchingStrings.html#//apple_ref/doc/uid/20000149) section?

bredell
Dec 1, 2009, 06:38 PM
NSScanner also does not provide claimed functionality.


NSString *html = @"<td>Hello world!</td>";
NSString *substring;

NSScanner *scanner = [[NSScanner alloc] initWithString:html];
[scanner scanUpToString:@"<td>" intoString:nil];
[scanner scanString:@"<td>" intoString:nil];
[scanner scanUpToString:@"</td>" intoString:&substring];

NSLog(@"Found string '%@'", substring);