美文网首页
iOS 中文数组排序

iOS 中文数组排序

作者: 114105lijia | 来源:发表于2019-08-25 11:17 被阅读0次

直接上代码:

NSArray *stringArr = @[@"我们",@"我的", @"重点", @"重庆",  @"三"];
NSArray *result = [stringArr sortedArrayUsingSelector:@selector(localizedCompare:)];
NSLog(@"%@", result);

汉子转拼音

+ (NSString *)pinyinFromHanzi:(NSString *)hanzi {
    
    CFStringTransform((CFMutableStringRef)hanzi, NULL, kCFStringTransformToLatin, false);
    CFStringTransform((CFMutableStringRef)hanzi, NULL, kCFStringTransformStripDiacritics, false);
    hanzi = [hanzi stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    return hanzi;
}

判断是否是为纯汉子:

- (BOOL)isChinese
{
    NSString *match = @"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}

判断是否含有汉子

- (BOOL)includeChinese
{
    for(int i=0; i< [self length];i++)
    {
        int a =[self characterAtIndex:i];
        if( a >0x4e00&& a <0x9fff){
            return YES;
        }
    }
    return NO;
}

相关文章

网友评论

      本文标题:iOS 中文数组排序

      本文链接:https://www.haomeiwen.com/subject/hpjhectx.html