For iOS the following code should work for numeric codes. It should be relatively easy to extend to the likes of &
...
-(NSString*)unescapeHtmlCodes:(NSString*)input {
NSRange rangeOfHTMLEntity = [input rangeOfString:@"&#"];
if( NSNotFound == rangeOfHTMLEntity.location ) {
return input;
}
NSMutableString* answer = [[NSMutableString alloc] init];
[answer autorelease];
NSScanner* scanner = [NSScanner scannerWithString:input];
[scanner setCharactersToBeSkipped:nil]; // we want all white-space
while( ![scanner isAtEnd] ) {
NSString* fragment;
[scanner scanUpToString:@"&#" intoString:&fragment];
if( nil != fragment ) { // e.g. '& B'
[answer appendString:fragment];
}
if( ![scanner isAtEnd] ) { // implicitly we scanned to the next '&#'
int scanLocation = (int)[scanner scanLocation];
[scanner setScanLocation:scanLocation+2]; // skip over '&#'
int htmlCode;
if( [scanner scanInt:&htmlCode] ) {
char c = htmlCode;
[answer appendFormat:@"%c", c];
scanLocation = (int)[scanner scanLocation];
[scanner setScanLocation:scanLocation+1]; // skip over ';'
} else {
// err ?
}
}
}
return answer;
}
Some unit-test code ...
-(void)testUnescapeHtmlCodes {
NSString* expected = @"A & B";
NSString* actual = [self unescapeHtmlCodes:@"A & B"];
STAssertTrue( [expected isEqualToString:actual], @"actual = %@", actual );
expected = @"& B";
actual = [self unescapeHtmlCodes:@"& B"];
STAssertTrue( [expected isEqualToString:actual], @"actual = %@", actual );
expected = @"A &";
actual = [self unescapeHtmlCodes:@"A &"];
STAssertTrue( [expected isEqualToString:actual], @"actual = %@", actual );
}