我正在使用服务器,我必须将文本下载到我的iOS应用程序。唯一的问题:例如“éàç”等所有字符都被“\ U008”取代。有没有办法解决这个问题,用正确的字符替换这段代码?
答案 0 :(得分:4)
尝试用以下方法解析收到的文本(textToParse变量):
NSString *encodedString = textToParse;
NSString *decodedString = [NSString stringWithUTF8String:[encodedString cStringUsingEncoding:[NSString defaultCStringEncoding]]];
答案 1 :(得分:1)
我测试了一些编码,NSMacOSRomanStringEncoding
很适合。
我的测试是:
NSString *encodedString = [NSString stringWithCString:"Você realmente deseja sair da área restrita" encoding:NSMacOSRomanStringEncoding];
请记住,邮件必须是C字符串("string"
),而不是NSString
(@"string"
)
答案 2 :(得分:0)
您可以获得字符缓冲区并验证每个字符,如下所示:
- (NSString *) removeUnicode:(NSString *) unicodeString {
NSUInteger len = [unicodeString length];
unichar buffer[len+1];
[unicodeString getCharacters:buffer range:NSMakeRange(0, len)];
unichar okBuffer[len+1];
int index = 0;
for(int i = 0; i < len; i++) {
if(buffer[i] < 128) {
okBuffer[index] = buffer[i];
index = index + 1;
}
}
NSString *removedUnicode = [[NSString alloc] initWithCharacters:okBuffer length:index];
return removedUnicode;
}
或者您可以使用此示例:
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:[NSCharacterSet alphanumericCharacterSet]] invertedSet];
stringWithOutUnicode = [[stringWithUnicode componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
您可以创建自己的有效字符集并获取不允许的字符
NSString *allowedCharacters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString: allowedCharacters] invertedSet];