简体   繁体   English

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

[英]How to remove/replace/show unicode characters from NSString

I'm parsing http://maps.google.com/maps/geo?q=48.209946,11.539421&output=csv . 我正在解析http://maps.google.com/maps/geo?q=48.209946,11.539421&output=csv The response is 200,8,"Lerchenstraße 66, 80995 Munich, Germany". 响应为200,8,“Lerchenstraße66,80995 Munich,Germany”。 I'm only showing the CityName ie, "Lerchenstraße 66". 我只显示CityName,即“Lerchenstraße66”。 But the problem is that the CityName string has Unicode characters, and it is showing like "Lerchenstra\\U00dfe66" in the view. 但是问题在于CityName字符串具有Unicode字符,并且在视图中显示为“ Lerchenstra \\ U00dfe66”。 The output should be like "Lerchenstrasse" or "Lerchenstraße". 输出应类似于“ 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);
}

Please help me. 请帮我。 Thanks in advance. 提前致谢。

I got the answer. 我得到了答案。 Thanks for the support and suggestions. 感谢您的支持和建议。 The code is: 代码是:

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);

And the output is "Lerchenstraße66". 输出为“Lerchenstraße66”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM