Index of Substring

Discussion in 'iOS Programming' started by MaDDaemoN, Dec 1, 2009.

  1. MaDDaemoN
    Expand Collapse
    macrumors newbie

    Joined:
    Dec 1, 2009
    #1
    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?
    Code:
    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?
     
  2. bredell
    Expand Collapse
    macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #2
    Take a look at the NSScanner class. Or read the "String Programming Guide for Cocoa".
     
  3. MaDDaemoN
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Dec 1, 2009
    #3
    I`ve read Cocoa string article already, but i did not find it helpfull. NSScanner also does not provide claimed functionality.
     
  4. dejo
    Expand Collapse
    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
  5. bredell
    Expand Collapse
    macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #5
    Code:
    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);
    
     

Share This Page