0
我已經編寫了一個功能,可以將文本「›
」,「«
」更改爲符號「›」 「«」,我想與stackoverflow用戶分享這個功能。如果你有建議如何使這個功能更好,請寫!謝謝 !!!如何將HTML代碼更改爲符號iphone
- (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;
}
你可以使用NSDictionary而不是數組? – 2011-04-28 06:45:51
首先,Objective-C中的方法名稱按照慣例以小寫字母開頭,就像任何其他編程語言(Visual Basic,AFAIK除外)一樣。然後你想小寫「&Dagger」。接下來,'&#xxx;'表示法可用於表示任何UTF字符,從0到65535(AFAIK),但您只覆蓋其中的一部分,不處理八進制或十六進制。所以總的來說,這個方法確實很方便,但需要注意的是它不完整,而且由於調用'stringByReplacingOccurrencesOfString:withString:'十幾次而效率很低。所以不要在長文本中使用它。 – DarkDust 2011-04-28 06:48:44
stringByReplacingOccurrencesOfString:withString:我可以做什麼? – 2011-04-28 06:50:37