iOS從文件讀取中午使用UTF-8編碼時自晰,部分復(fù)雜的中文會占用4-6個字節(jié)凝化。如果按1位來分割加入數(shù)組就會出現(xiàn)錯誤。如 ?? 字酬荞,會占用兩個長度搓劫。
NSString *files = [[NSBundle bundleWithPath:_bundlePath] pathForResource:@"cn_1.1" ofType:@"txt"];
NSString *lines = [[NSString stringWithContentsOfFile:files encoding:NSUTF8StringEncoding error:nil] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSMutableArray *characterArray = [NSMutableArray array];
NSRange range = NSMakeRange(0, 1);
for(int i = 0; i < lines.length; i += range.length){
range.location = i;
NSString *character = [lines substringWithRange:range];
[characterArray addObject:character];
}
使用[str rangeOfComposedCharacterSequenceAtIndex:i]方法可以獲得改文字的真實長度,用于分割可以得到正確的結(jié)果
NSString *files = [[NSBundle bundleWithPath:_bundlePath] pathForResource:@"cn_1.1" ofType:@"txt"];
NSString *lines = [[NSString stringWithContentsOfFile:files encoding:NSUTF8StringEncoding error:nil] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSMutableArray *characterArray = [NSMutableArray array];
NSRange range = NSMakeRange(0, 1);
for(int i = 0; i < lines.length; i += range.length){
range = [lines rangeOfComposedCharacterSequenceAtIndex:i];
NSString *character = [lines substringWithRange:range];
[characterArray addObject:character];
}