我编写了一个函数,将“›
”,“«
”等文本更改为符号“>”“«”,我希望与stackoverflow用户共享此函数。如果您有如何使这个功能更好的建议请写!!!谢谢!!!
- (NSString*) ChangeAccentsLettersToSymbols: (NSString*) strToCorrect {
NSLog(@"ChangeAccentsLettersToSymbols Entered\n");
static NSString * const codeMap[][2] = {
{@"¡", @"¡"}, {@"«", @"«"}, {@"»", @"»"}, {@"‹", @"‹"},
{@"›", @"›"}, {@"‚", @"‚"}, {@"„", @"„"}, {@"“", @"“"},
{@"”", @"”"}, {@"‘", @"‘"}, {@"’", @"’"}, {@"¢", @"¢"},
{@"£", @"£"}, {@"¥", @"¥"}, {@"€", @"€"}, {@"¤", @"¤"},
{@"ƒ", @"ƒ"}, {@">", @">"}, {@"<", @"<"}, {@"÷", @"÷"},
{@"°", @"°"}, {@"¬", @"¬"}, {@"±", @"±"}, {@"µ", @"µ"},
{@"&", @"&"}, {@"®", @"®"}, {@"©", @"©"}, {@"™", @"™"},
{@"•", @"•"}, {@"·", @"·"}, {@"§", @"§"}, {@"–", @"–"},
{@"—", @"—"}, {@"†", @"†"}, {@"‡", @"‡"}, {@"◊", @"◊"},
{@"↑", @"↑"}, {@"↓", @"↓"}, {@"←", @"←"}, {@"→", @"→"},
{@"↔", @"↔"}, {@"¿", @"¿"}, {@" ", @" "}, {@""", @"\""}
};
int count = sizeof(codeMap)/sizeof(codeMap[0]);
for( int i=0; i<count; ++i ) {
strToCorrect = [ strToCorrect stringByReplacingOccurrencesOfString: codeMap[i][0]
withString: codeMap[i][1] ];
}
for( int i=33; i<126; ++i) {
NSString* whotToReplace = [NSString stringWithFormat:@"&#%d;", i];
NSString* replaceWith = [NSString stringWithFormat:@"%c", (char*)i ];
strToCorrect = [strToCorrect stringByReplacingOccurrencesOfString: whotToReplace
withString: replaceWith ];
}
return strToCorrect;
}
答案 0 :(得分:2)
github上有回购 -
https://github.com/mwaterfall/MWFeedParser
看看这个功能
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
它对我来说很好。