objective c - NSString with wrong characters when using UTF-8 -
i data utf-8 encoded xml file, , want display every element in uitableview . want cells have same size , display 2 first text lines data possible tried remove carriage returns.
in cellforrow method changed :
[[mycell textlabel] settext:data];
by :
[[mycell textlabel] settext:[self correctdata:data]];
here correctdata method :
- (nsstring *) correctdata : (nsstring *) str { nsmutablestring *res = [nsmutablestring stringwithformat:@""]; for(int = 0 ; < [str length] ; i++) { char car = [str characteratindex:i]; if(car != 10 && car != 13) [res appendstring:[nsstring stringwithformat:@"%c",car]]; } return res; }
and correctly removes carriage returns, alters utf-8 chars. example, bit of initial string (str) :
diplômé(e) d'etat
and function becomes :
diplÙmÈ(e) d'etat
what should ? thanks.
nsstring
works unichar
characters stored on 16bits whereas char
8bits long. converting unichar
char
alter characters code point above u+00ff
.
you can solve issue replacing char
unichar
, %c
%c
.
edit: that's not efficient. may better use regular expressions replace newline characters in once:
str = [str stringbyreplacingoccurrencesofstring:@"[\r\n]+" withstring:@"" options:nsregularexpressionsearch range:nsmakerange(0, str.length)];
Comments
Post a Comment