Wednesday, 16 May 2012

Unable to strip excess newline characters from a string or an array :/


hi guys,

have information pulled webpage. stripped html string , looks (its long part of it):

code:
          beginner                    upper mambo alley        yes        yes                  lower mambo alley        yes        yes                  snow drop - beginner's area        yes        yes   
the problem is, trying this:

code:
       upper mambo alley        yes        yes        lower mambo alley        yes        yes         snow drop - beginner's area        yes        yes   
for reason seems impossible task in objective-c. here things have tried before posting here:

1. tried using nsscanner scan 2 consecutive newline characters. no luck.

2. tried using stringbyreplacingoccurancesofstring@"\n\n" withstring: ""];

3. tried reading string array using nsarray *testcontents = [strippedsitedata componentsseparatedbystring:mad:"\n"];, converting nsmutablearray , comparing contents , removing array member newline character.

nothing seems working.

guessing 1 of 2 things, either comparison statement wrong (where code says "this not working") or other newline characters in these strings or in array created.

if can give me heads wrong appreciated. here code:

code:
  #import <foundation/foundation.h>    //function prototypes  nsstring *striphtml(nsstring *html);  nsstring *removerandomtags(nsstring *html);        int main (int argc, const char * argv[])  {        @autoreleasepool {                    //create url          nsurl *url = [nsurl urlwithstring:@"http://www.blueknob.com/winter/conditions.php"];                    //request information website          nsurlrequest *request = [nsurlrequest requestwithurl:url];          nserror *error = nil;          nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:null error:&error];                    //check if data read          if(!data)          {              nslog(@"request failed %@", [error localizeddescription]);              return 1;          }                    //convert nsdata object nsstring          nsstring *sitedata = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];                    //strip html tags , random html tags          nsstring *strippedsitedata = [[nsstring alloc] initwithstring:striphtml(sitedata)];          strippedsitedata = removerandomtags(strippedsitedata);                    //check output          nslog(@"%@", strippedsitedata);                    //create array based on data          nsarray *testcontents = [strippedsitedata componentsseparatedbystring:@"\n"];          nsmutablearray *contents = [nsmutablearray arraywitharray:testcontents];                    //attempt remove objects newline characters          for(int = 0; < [contents count]; ++i)          {              if([contents objectatindex:i] == @"\n") //this doesn't work                  [contents removeobjectatindex:i];          }                    //print contents          for(nsstring *s in contents)          {              nslog(@"%@", s);          }      }      return 0;  }    nsstring *striphtml(nsstring *html)  {      //scan string , strip out html      nsscanner *scanner = [nsscanner scannerwithstring:html];      nsstring *text = nil;            while([scanner isatend] == no)      {          //beginning of tag          [scanner scanuptostring:@"<" intostring:nil];                    //end of tag          [scanner scanuptostring:@">" intostring:&text];                    //replace found tag space          html = [html stringbyreplacingoccurrencesofstring:[nsstring stringwithformat:@"%@>", text] withstring:@" "];      }            return html;  }    nsstring *removerandomtags(nsstring *html)  {      nsscanner *scanner = [nsscanner scannerwithstring:html];      nsstring *text = nil;            while([scanner isatend] == no)      {          //beginning of tag          [scanner scanuptostring:@"&" intostring:nil];                    //end of tag          [scanner scanuptostring:@";" intostring:&text];                    //replace found tag nothing          html = [html stringbyreplacingoccurrencesofstring:[nsstring stringwithformat:@"%@;", text] withstring:@""];      }            return html;  }  
 

are sure \n characters? there other return characters (\r example).
 


Forums iPhone, iPad, and iPod Touch iOS Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment