标签:
汉字转拼音
NSMutableString *ms = [[NSMutableString alloc] initWithString:@"我是中国人"]; if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) { NSLog(@"Pingying: %@", ms); // wǒ shì zhōng guó rén } if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) { NSLog(@"Pingying: %@", ms); // wo shi zhong guo ren }
中文拼音排序器
#define GB18030_ENCODING CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000) + (NSComparator)compor { return ^NSComparisonResult(NSString *obj1, NSString *obj2) { NSString *str1 = [obj1 stringByAddingPercentEscapesUsingEncoding:GB18030_ENCODING]; NSString *str2 = [obj2 stringByAddingPercentEscapesUsingEncoding:GB18030_ENCODING]; return [str1 compare:str2]; }; }
原理:中文gb编码本来就是用拼音排序的就hack了一下:在stringByAddingPercentEscapesUsingEncoding:后面用16位编码 将中文转为ascii来比较 更简洁。
标签:
原文地址:http://www.cnblogs.com/songxing10000/p/4732373.html