zoukankan      html  css  js  c++  java
  • 汉字转拼音 汉字排序功能

    .h文件

    #import <Foundation/Foundation.h>

     

    @interface ChineseTool : NSObject

     

    /**

    汉字转拼音

    *

    *  @param chinese         要转换的汉字

    *  @param stripDiacritics 是否需要音标

    *

    *  @return 拼音

    */

    + (NSString *)pinyinForChinese:(NSString *)chinese stripDiacritics:(BOOL)stripDiacritics;

     

    /**

     *  对汉字数组进行排序

     *

     *  @param chineseArr 汉字数组

     *

     *  @return 排好序的数组

     */

    + (NSArray *)sortForChineseArr:(NSArray *)chineseArr;

     

    @end

     

    .m文件

    #import "ChineseTool.h"

     

    @implementation ChineseTool

     

    + (NSString *)pinyinForChinese:(NSString *)chinese stripDiacritics:(BOOL)stripDiacritics

    {

        if ([chinese length]) {

            NSMutableString *ms = [[NSMutableString alloc] initWithString:chinese];

            if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {

                if (stripDiacritics) {

                    if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {

                        return ms;

                    }

                } else {

                    return ms;

                }

            }

        }

        return nil;

    }

     

    + (NSArray *)sortForChineseArr:(NSArray *)chineseArr

    {

        NSMutableArray *pinyinArr = [NSMutableArray arrayWithCapacity:0];

        for (NSString *str in chineseArr) {

            NSString *pinyin = [[self pinyinForChinese:str stripDiacritics:YES] stringByReplacingOccurrencesOfString:@" " withString:@""];

            [pinyinArr addObject:pinyin];

        }

        NSDictionary *dict = [NSDictionary dictionaryWithObjects:chineseArr forKeys:pinyinArr];

        NSArray *sortPinyinArr = [pinyinArr sortedArrayUsingSelector:@selector(compare:)];

        NSArray *chineseSortArr = [dict objectsForKeys:sortPinyinArr notFoundMarker:[NSNull null]];

        return chineseSortArr;

    }

  • 相关阅读:
    1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
    1389. Create Target Array in the Given Order
    1385. Find the Distance Value Between Two Arrays
    1382. Balance a Binary Search Tree
    PHP的 __DIR__ 作用【转】
    PHP:Allowed memory size of 134217728 bytes exhausted问题解决方法【转】
    PHP: POST Content-Length of xxx bytes exceeds the limit of 8388608 bytes【转】
    PhpStorm 如何快速查找文件【转】
    .gitkeep的作用【转】
    php copy 函数使用 【转】
  • 原文地址:https://www.cnblogs.com/bing-ge/p/4744722.html
Copyright © 2011-2022 走看看