Hi guys,
I have information I pulled from a webpage. I stripped the HTML from it so now it is just a string and looks like this (its long so this is only part of it):
The problem is, I am trying to get it to look like this:
For some reason this seems like an impossible task in Objective-C. Here are the things I have tried before posting here:
1. I tried using NSScanner to scan for two consecutive newline characters. No luck.
2. I tried using stringByReplacingOccurancesOfString@"\n\n" withString: ""];
3. I tried reading the string into an array using NSArray *testContents = [strippedSiteData componentsSeparatedByString"\n"];, converting it to an NSMutableArray and then comparing the contents and removing any array member that was a newline character.
Nothing seems to be working.
I am guessing one of two things, either my comparison statement is wrong (where the code says "this is not working") or its something other than newline characters that are in these strings or in the array created.
If anyone can give me a heads up to what is wrong it would be greatly appreciated. Here is my code:
I have information I pulled from a webpage. I stripped the HTML from it so now it is just a string and looks like this (its long so this is only 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, I am trying to get it to look like this:
Code:
Upper Mambo Alley
Yes
Yes
Lower Mambo Alley
Yes
Yes
Snow Drop - Beginner's Area
Yes
Yes
For some reason this seems like an impossible task in Objective-C. Here are the things I have tried before posting here:
1. I tried using NSScanner to scan for two consecutive newline characters. No luck.
2. I tried using stringByReplacingOccurancesOfString@"\n\n" withString: ""];
3. I tried reading the string into an array using NSArray *testContents = [strippedSiteData componentsSeparatedByString"\n"];, converting it to an NSMutableArray and then comparing the contents and removing any array member that was a newline character.
Nothing seems to be working.
I am guessing one of two things, either my comparison statement is wrong (where the code says "this is not working") or its something other than newline characters that are in these strings or in the array created.
If anyone can give me a heads up to what is wrong it would be greatly appreciated. Here is my 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 from website
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:&error];
//Check if data was read
if(!data)
{
NSLog(@"Request failed %@", [error localizedDescription]);
return 1;
}
//Convert NSData object to an NSString
NSString *siteData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//Strip HTML tags and random HTML tags
NSString *strippedSiteData = [[NSString alloc] initWithString:stripHTML(siteData)];
strippedSiteData = removeRandomTags(strippedSiteData);
//Check output
NSLog(@"%@", strippedSiteData);
//Create an array based on data
NSArray *testContents = [strippedSiteData componentsSeparatedByString:@"\n"];
NSMutableArray *contents = [NSMutableArray arrayWithArray:testContents];
//Attempt to remove any objects that are only newline characters
for(int i = 0; i < [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 the string and strip out the HTML from it
NSScanner *scanner = [NSScanner scannerWithString:html];
NSString *text = nil;
while([scanner isAtEnd] == NO)
{
//Beginning of a tag
[scanner scanUpToString:@"<" intoString:nil];
//End of a tag
[scanner scanUpToString:@">" intoString:&text];
//Replace the found tag with a 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 a tag
[scanner scanUpToString:@"&" intoString:nil];
//End of a tag
[scanner scanUpToString:@";" intoString:&text];
//Replace the found tag with nothing
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@;", text] withString:@""];
}
return html;
}