user2255567
user2255567

Reputation: 3

Removing HTML from NSString causes issues

I made a RSS reader, and I'm parsing the description, but there are HTML tags in the description, so I created a category of NSString with the following method to weed out the tags:

- (NSString *)stripTags:(NSString *)str
{
NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];

NSScanner *scanner = [NSScanner scannerWithString:str];
scanner.charactersToBeSkipped = NULL;
[scanner setCharactersToBeSkipped:nil];
NSString *tempText = nil;

while (![scanner isAtEnd])
{
    [scanner scanUpToString:@"<" intoString:&tempText];

    if (tempText != nil)
        [html appendString:tempText];

    [scanner scanUpToString:@">" intoString:NULL];

    if (![scanner isAtEnd])
        [scanner setScanLocation:[scanner scanLocation] + 1];

    tempText = nil;
}

return html;
}

This works well in removing the HTML tags, that's not the issue. The issue is that I have the description set to be a maximum of 100 characters in length, but it's still counting the removed HTML tags in that character count. So some descriptions don't show up at all, or some are very short. I need to know how I can remove the HTML tags so that they don't take up any of the character count.

If you need, here's where I'm setting my description:

NSString *dots;
int length = [self.description length];
if (length > 100)
{
    length = 100;
    dots = [NSString stringWithFormat:@"..."];
}
else
{
    dots = [NSString stringWithFormat:@""];
}

NSString *description = [NSString stringWithFormat:@"%@%@", [self.description substringToIndex:length], dots];

Upvotes: 0

Views: 103

Answers (1)

Ben Coffman
Ben Coffman

Reputation: 1740

This appears to be because you set the capacity of the string from the original string with your first line.

NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];

I believe this sets a minimum capacity size and while it can grow beyond this freely it cannot shrink to your new size.

A quick fix would probably be to set the length initially to 1 or something smaller than the smallest expected text.

Example:

NSMutableString *html = [NSMutableString stringWithCapacity:1];

You could simply use:

NSMutableString *html = [[NSMutableString alloc] init];

I tried with this code and it works exactly like it should

NSMutableString *html = [[NSMutableString alloc] init];
NSLog(@"Before: %u", [html length]);

NSScanner *scanner = [NSScanner scannerWithString:@"<a>this is a test</a>"];
scanner.charactersToBeSkipped = NULL;
[scanner setCharactersToBeSkipped:nil];
NSString *tempText = nil;

while (![scanner isAtEnd])
{
    [scanner scanUpToString:@"<" intoString:&tempText];

    if (tempText != nil)
        [html appendString:tempText];

    [scanner scanUpToString:@">" intoString:NULL];

    if (![scanner isAtEnd])
        [scanner setScanLocation:[scanner scanLocation] + 1];

    tempText = nil;
}

NSLog(@"After: %u", [html length]);

Upvotes: 1

Related Questions