中英文字符计算

时间:2022-04-27 20:12:24

//按照中文两个字符,英文数字一个字符计算字符数

-(NSUInteger)unicodeLengthOfString:(NSString *)text {

    NSUInteger asciiLength =0;

    for (NSUInteger i =0; i < text.length; i++) {

        unichar uc = [textcharacterAtIndex: i];

        asciiLength +=isascii(uc) ? 1 :2;

    }

    

    NSLog(@"字符长度为: %@",@(asciiLength));

    return asciiLength;

}