计算字符串长度

-(NSUInteger) unicodeLengthOfString: (NSString *) text//汉字长度  2个英文字母=1个汉字

{

    NSUInteger asciiLength = 0;

    

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

    {

        unichar uc = [text characterAtIndex: i];

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

    }

    NSUInteger unicodeLength = asciiLength / 2;

    if(asciiLength % 2)

    {

        unicodeLength++;

    }

    return unicodeLength;

}

-(NSUInteger) asciiLengthOfString: (NSString *) text//一个汉字2字节

{

    NSUInteger asciiLength = 0;

    

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

    {

        unichar uc = [text characterAtIndex: i];

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

    }

    return asciiLength;

}

原文地址:https://www.cnblogs.com/swallow37/p/3060865.html