版本1:
NSString *hello = @"hello world";
for (int i = 0 ; i < hello.length; i ++) {
unichar charactor = [hello characterAtIndex:i];
NSLog(@"%C",charactor);
}
上面的方法在一些字符如😊😢等在字符串中時則不適用了
改進版 : rangeOfComposedCharacterSequenceAtIndex:能夠獲取到完整字的范圍
NSString是utf-16編碼(?不確定是不是),但有一部分字符需要用2個16位字符才能表示
NSRange range;
for (int i = 0; i < hello.length ; i += range.length ) {
unichar chara = [hello characterAtIndex:i];
range = [hello rangeOfComposedCharacterSequenceAtIndex:i];
NSString *subStr = [hello substringWithRange:range];
NSLog(@"%@ %@",subStr,NSStringFromRange(range));
}
