Created
August 26, 2017 09:40
-
-
Save Mahabali/aaa639594ab25a797341694e064ec747 to your computer and use it in GitHub Desktop.
iOS HTML characters decoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (NSString*)decodeHTMLCharacters:(NSString*)sampleUnFormatterString{ | |
if (sampleUnFormatterString == nil || sampleUnFormatterString.length == 0) { | |
return sampleUnFormatterString; | |
} | |
NSString *sampleUnFormatterStringCopy =sampleUnFormatterString; | |
NSString *regString = [[NSString alloc]initWithFormat:@"&#[0123456789]{2,4};"]; | |
NSError *error=nil; | |
NSRegularExpression *regex = [NSRegularExpression | |
regularExpressionWithPattern:regString | |
options:0 | |
error:&error]; | |
NSArray* matches = [regex matchesInString:sampleUnFormatterString options:0 range: NSMakeRange(0, [sampleUnFormatterString length])]; | |
for (NSTextCheckingResult* match in matches) { | |
NSString* matchText = [sampleUnFormatterString substringWithRange:[match range]]; | |
NSString *strippedText = [matchText stringByReplacingOccurrencesOfString:@"&#" withString:@""]; | |
strippedText = [strippedText stringByReplacingOccurrencesOfString:@";" withString:@""]; | |
strippedText = [NSString stringWithFormat:@"%C",(unichar)strippedText.integerValue]; | |
sampleUnFormatterStringCopy= [sampleUnFormatterStringCopy stringByReplacingOccurrencesOfString:matchText withString:strippedText]; | |
} | |
NSLog(@"Formatted String %@",sampleUnFormatterStringCopy); | |
return sampleUnFormatterStringCopy; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment