如何从NSString中删除/替换/显示unicode字符

时间:2012-11-29 06:00:29

标签: objective-c ios xcode unicode

我正在解析http://maps.google.com/maps/geo?q=48.209946,11.539421&output=csv。答复是200,8,“Lerchenstraße66,80995慕尼黑,德国”。我只展示CityName,即“Lerchenstraße66”。但问题是CityName字符串有Unicode字符,它在视图中显示为“Lerchenstra \ U00dfe66”。输出应该像“Lerchenstrasse”或“Lerchenstraße”。

-(void) httpDataDidFinishLoadingWithData:(NSData *)theData {
  m_activityLoaded=NO;
  temp=[[NSString alloc] initWithData:[dataLoader httpData] encoding:NSUTF8StringEncoding];
  NSArray *listItems = [temp componentsSeparatedByString:@","];
  NSArray *tempCountryArray = [[listItems objectAtIndex:[listItems count]-3] componentsSeparatedByString:@"\""];
  NSString *string = [NSString stringWithFormat:@"%@", tempCountryArray];
  NSString *strTe=[[[[string
    stringByReplacingOccurrencesOfString:@"(" withString:@""]
    stringByReplacingOccurrencesOfString:@")" withString:@""]
    stringByReplacingOccurrencesOfString:@" " withString:@""]
    stringByReplacingOccurrencesOfString:@"\"" withString:@"" ];
  self.cityNameString = strTe;
  NSLog(@"CITY NAME IS : %@", strTe);
}

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:-1)

我得到了答案。感谢您的支持和建议。代码是:

NSString* esc1 = [strTe stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
NSString* esc2 = [esc1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString* quoted = [[@"\"" stringByAppendingString:esc2] stringByAppendingString:@"\""];
NSData* data = [quoted dataUsingEncoding:NSUTF8StringEncoding];
NSString* unesc = [NSPropertyListSerialization propertyListFromData:data
  mutabilityOption:NSPropertyListImmutable format:NULL
  errorDescription:NULL];
assert([unesc isKindOfClass:[NSString class]]);
NSLog(@"Output = %@", unesc);

输出是“Lerchenstraße66”。