Is there a better way to deal with reserved characters when parsing XML/JSON data on the iPhone?

Posted by Charles S. on Stack Overflow See other posts from Stack Overflow or by Charles S.
Published on 2010-03-24T02:25:04Z Indexed on 2010/03/24 2:33 UTC
Read the original article Hit count: 235

Filed under:
|
|
|
|

The following code works, but it's ugly and creates a bunch of autoreleased objects. I'm using similar code for parsing reserved HTML characters as well (for quotes, & symbols, etc). I'm just wondering... Is there a cleaner way?

    NSString *result = [[NSString alloc] initWithString:userInput];

    NSString *result2 = [result stringByReplacingOccurrencesOfString:@"#" 
                                withString:@"\%23"];
    NSString *result3 = [result2 stringByReplacingOccurrencesOfString:@" " 
                                withString:@"\%20"];
    formatted = [[result3 stringByReplacingOccurrencesOfString:@"&" 
                                withString:@"\%26"] retain];
    [result release];

© Stack Overflow or respective owner

Related posts about iphone

Related posts about parse